Added API to retrieve the current set of processes on the system
authorDavide Libenzi <dlibenzi@google.com>
Tue, 27 Oct 2015 22:04:15 +0000 (15:04 -0700)
committerBarret Rhoden <brho@cs.berkeley.edu>
Wed, 18 Nov 2015 17:54:57 +0000 (09:54 -0800)
Added API to retrieve the current set of processes on the system.

Signed-off-by: Davide Libenzi <dlibenzi@google.com>
Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
kern/include/process.h
kern/src/process.c

index 2aab783..9ef1c5c 100644 (file)
 
 #include <env.h>
 
+struct process_set {
+       size_t num_processes;
+       size_t size;
+       struct proc **procs;
+};
+
 /* Can use a htable iterator to iterate through all active procs */
 extern struct hashtable *pid_hash;
 extern spinlock_t pid_hash_lock;
@@ -91,6 +97,8 @@ bool __proc_is_mcp(struct proc *p);
 bool proc_is_vcctx_ready(struct proc *p);
 int proc_change_to_vcore(struct proc *p, uint32_t new_vcoreid,
                          bool enable_my_notif);
+void proc_get_set(struct process_set *pset);
+void proc_free_set(struct process_set *pset);
 
 /* Vcoremap info: */
 uint32_t proc_get_vcoreid(struct proc *p);
index b8a6f75..1f54bb1 100644 (file)
@@ -2245,6 +2245,48 @@ void print_allpids(void)
        spin_unlock(&pid_hash_lock);
 }
 
+void proc_get_set(struct process_set *pset)
+{
+       void enum_proc(void *item, void *opaque)
+       {
+               struct proc *p = (struct proc*) item;
+               struct process_set *pset = (struct process_set *) opaque;
+
+               if (pset->num_processes < pset->size) {
+                       proc_incref(p, 1);
+
+                       pset->procs[pset->num_processes] = p;
+                       pset->num_processes++;
+               }
+       }
+
+       static const size_t num_extra_alloc = 16;
+
+       pset->procs = NULL;
+       do {
+               if (pset->procs)
+                       proc_free_set(pset);
+               pset->size = atomic_read(&num_envs) + num_extra_alloc;
+               pset->num_processes = 0;
+               pset->procs = (struct proc **)
+                       kzmalloc(pset->size * sizeof(struct proc *), KMALLOC_WAIT);
+               if (!pset->procs)
+                       error(-ENOMEM, NULL);
+
+               spin_lock(&pid_hash_lock);
+               hash_for_each(pid_hash, enum_proc, pset);
+               spin_unlock(&pid_hash_lock);
+
+       } while (pset->num_processes == pset->size);
+}
+
+void proc_free_set(struct process_set *pset)
+{
+       for (size_t i = 0; i < pset->num_processes; i++)
+               proc_decref(pset->procs[i]);
+       kfree(pset->procs);
+}
+
 void print_proc_info(pid_t pid)
 {
        int j = 0;