int ps_get_pid(int position) { if(cur_ps_count < position || position < 0) return 0; process_info *current; if(ps_list_setposition(&cur_ps_list, ¤t, position) != 0) return current->pid; return 0; }
void ps_get_name(int position, char *buf) { if (work_ps_count < position || position < 0) { buf[0] = 0; return; } process_info *current; if (ps_list_setposition(&work_ps_list, ¤t, position) != 0) strncpy(buf, current->name, BUFFERSIZE); else buf[0] = 0; return; }
void ps_get_status(int position, char *buf) { if (work_ps_count < position || position < 0) { buf[0] = 0; return; } process_info *current; if (ps_list_setposition(&work_ps_list, ¤t, position) != 0) snprintf(buf, BUFFERSIZE, "%c", current->status); else snprintf(buf, BUFFERSIZE, "%c", 0); return; }
int ps_info(int position, char *bufName, char *bufStatus) { bufName[0] = 0; bufStatus[0] = 0; if (work_ps_count < position || position < 0) { return 0; } process_info *current; if (ps_list_setposition(&work_ps_list, ¤t, position) != 0) { strncpy(bufName, current->name, BUFFERSIZE); snprintf(bufStatus, BUFFERSIZE, "%c", current->status); } return 1; }
void ps_get_name_by_uid(int uid, char *buf) { int search_ps = 0; process_info *current; buf[0] = 0; while( cur_ps_count > search_ps) { if(!ps_list_setposition(&cur_ps_list, ¤t, search_ps) != 0) break; if(current->uid == uid) { strncpy(buf, current->name, BUFFERSIZE); break; } search_ps++; } return; }
void ps_refresh_load() { if(old_ps_count == 0) return; unsigned long pre_cpu_time = pre_cpu.user + pre_cpu.nice + pre_cpu.system + pre_cpu.idle + pre_cpu.iowait + pre_cpu.irq + pre_cpu.softirq; unsigned long cur_cpu_time = cur_cpu.user + cur_cpu.nice + cur_cpu.system + cur_cpu.idle + cur_cpu.iowait + cur_cpu.irq + cur_cpu.softirq; unsigned long total_delta_time = cur_cpu_time - pre_cpu_time; cpu_usage = 0; unsigned long ps_total_delta_time = 0; int work_psptr, old_psptr, ps_load, use_time; process_info *work, *old; for(work_psptr = 0; work_psptr < work_ps_count; work_psptr++) { for(old_psptr = 0; old_psptr < old_ps_count; old_psptr++) { if(ps_list_setposition(&work_ps_list, &work, work_psptr) == 0) continue; if(ps_list_setposition(&old_ps_list, &old, old_psptr) == 0) continue; if(old->pid == work->pid) { unsigned long delta_stime = work->delta_stime - old->delta_stime; unsigned long delta_utime = work->delta_utime - old->delta_utime; ps_total_delta_time = ps_total_delta_time + delta_stime + delta_utime; old_psptr = old_ps_count; break; } } } if(ps_total_delta_time > total_delta_time) total_delta_time = ps_total_delta_time; for(work_psptr = 0; work_psptr < work_ps_count; work_psptr++) { for(old_psptr = 0; old_psptr < old_ps_count; old_psptr++) { if(ps_list_setposition(&work_ps_list, &work, work_psptr) == 0) continue; if(ps_list_setposition(&old_ps_list, &old, old_psptr) == 0) continue; if(old->pid == work->pid) { unsigned long delta_stime = work->delta_stime - old->delta_stime; unsigned long delta_utime = work->delta_utime - old->delta_utime; old->delta_load = (delta_stime + delta_utime) * 100 / total_delta_time;; ps_load = (int) old->delta_load; if(ps_load > 100 || ps_load < 0) ps_load = 0; old->load = ps_load; cpu_usage += old->load; old_psptr = old_ps_count; break; } } } }
void ps_bubblesort(process_info **work_list, int n) { int i, j, flag, doswitch; process_info *current, *next; flag = 1; for (i = 1 ; i < n && flag; i++) { flag = 0; for (j = 0; j < n - 1; j++) { doswitch = 0; if(ps_list_setposition(work_list, ¤t, j) == 0) continue; if(ps_list_setposition(work_list, &next, j+1) ==0) continue; switch(ps_sort_type) { case byPID: if(ps_sort_direction == 0) { if (next->pid > current->pid) doswitch = 1; } else { if (next->pid < current->pid) doswitch = 1; } break; case byLoad: if(ps_sort_direction == 0) { if (next->delta_load > current->delta_load) doswitch = 1; } else { if (next->delta_load < current->delta_load) doswitch = 1; } break; case byMem: if(ps_sort_direction == 0) { if (next->rss > current->rss) doswitch = 1; } else { if (next->rss < current->rss) doswitch = 1; } break; case byThreads: if(ps_sort_direction == 0) { if (next->threadnum > current->threadnum) doswitch = 1; } else { if (next->threadnum < current->threadnum) doswitch = 1; } break; case byName: if(ps_sort_direction == 0) { if(strcmp(next->name, current->name) < 0) doswitch = 1; } else { if(strcmp(next->name, current->name) > 0) doswitch = 1; } } if(doswitch == 1) { ps_instance_swap(next, current); flag = 1; } } } }
void ps_quicksort(process_info **work_list, int left, int right) { int i, j, doswitch; process_info *pivot, *current, *swapi, *swapj; if (left >= right) { return; } ps_list_setposition(work_list, &pivot, left); i = left+1; j = right; while(1) { while(i <= right) { doswitch = 0; ps_list_setposition(work_list, ¤t, i); switch(ps_sort_type) { case byPID: if(ps_sort_direction == 0) { if (pivot->pid > current->pid) doswitch = 1; } else { if (pivot->pid < current->pid) doswitch = 1; } break; case byLoad: if(ps_sort_direction == 0) { if (pivot->delta_load > current->delta_load) doswitch = 1; } else { if (pivot->delta_load < current->delta_load) doswitch = 1; } break; case byMem: if(ps_sort_direction == 0) { if (pivot->rss > current->rss) doswitch = 1; } else { if (pivot->rss < current->rss) doswitch = 1; } break; case byThreads: if(ps_sort_direction == 0) { if (pivot->threadnum > current->threadnum) doswitch = 1; } else { if (pivot->threadnum < current->threadnum) doswitch = 1; } break; case byName: if(ps_sort_direction == 0) { if(strcmp(pivot->name, current->name) < 0) doswitch = 1; } else { if(strcmp(pivot->name, current->name) > 0) doswitch = 1; } } if(doswitch == 1) break; i = i +1; } while(j > left) { doswitch = 0; ps_list_setposition(work_list, ¤t, j); switch(ps_sort_type) { case byPID: if(ps_sort_direction != 0) { if (pivot->pid > current->pid) doswitch = 1; } else { if (pivot->pid < current->pid) doswitch = 1; } break; case byLoad: if(ps_sort_direction != 0) { if (pivot->delta_load > current->delta_load) doswitch = 1; } else { if (pivot->delta_load < current->delta_load) doswitch = 1; } break; case byMem: if(ps_sort_direction != 0) { if (pivot->rss > current->rss) doswitch = 1; } else { if (pivot->rss < current->rss) doswitch = 1; } break; case byThreads: if(ps_sort_direction != 0) { if (pivot->threadnum > current->threadnum) doswitch = 1; } else { if (pivot->threadnum < current->threadnum) doswitch = 1; } break; case byName: if(ps_sort_direction != 0) { if(strcmp(pivot->name, current->name) < 0) doswitch = 1; } else { if(strcmp(pivot->name, current->name) > 0) doswitch = 1; } } if(doswitch == 1) break; j = j -1; } if(i > j) break; ps_list_setposition(work_list, &swapi, i); ps_list_setposition(work_list, &swapj, j); ps_instance_swap(swapi, swapj); } ps_list_setposition(work_list, &swapj, j); ps_list_setposition(work_list, &swapi, left); ps_instance_swap(swapi, swapj); ps_quicksort(work_list, left, j-1); ps_quicksort(work_list, j+1, right); }