ProcessTable.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. htop - ProcessTable.c
  3. (C) 2004,2005 Hisham H. Muhammad
  4. Released under the GNU GPLv2+, see the COPYING file
  5. in the source distribution for its full text.
  6. */
  7. #include "config.h" // IWYU pragma: keep
  8. #include "ProcessTable.h"
  9. #include <assert.h>
  10. #include <stdlib.h>
  11. #include "Hashtable.h"
  12. #include "Row.h"
  13. #include "Settings.h"
  14. #include "Vector.h"
  15. void ProcessTable_init(ProcessTable* this, const ObjectClass* klass, Machine* host, Hashtable* pidMatchList) {
  16. Table_init(&this->super, klass, host);
  17. this->pidMatchList = pidMatchList;
  18. }
  19. void ProcessTable_done(ProcessTable* this) {
  20. Table_done(&this->super);
  21. }
  22. Process* ProcessTable_getProcess(ProcessTable* this, pid_t pid, bool* preExisting, Process_New constructor) {
  23. const Table* table = &this->super;
  24. Process* proc = (Process*) Hashtable_get(table->table, pid);
  25. *preExisting = proc != NULL;
  26. if (proc) {
  27. assert(Vector_indexOf(table->rows, proc, Row_idEqualCompare) != -1);
  28. assert(Process_getPid(proc) == pid);
  29. } else {
  30. proc = constructor(table->host);
  31. assert(proc->cmdline == NULL);
  32. Process_setPid(proc, pid);
  33. }
  34. return proc;
  35. }
  36. static void ProcessTable_prepareEntries(Table* super) {
  37. ProcessTable* this = (ProcessTable*) super;
  38. this->totalTasks = 0;
  39. this->userlandThreads = 0;
  40. this->kernelThreads = 0;
  41. this->runningTasks = 0;
  42. Table_prepareEntries(super);
  43. }
  44. static void ProcessTable_iterateEntries(Table* super) {
  45. ProcessTable* this = (ProcessTable*) super;
  46. // calling into platform-specific code
  47. ProcessTable_goThroughEntries(this);
  48. }
  49. static void ProcessTable_cleanupEntries(Table* super) {
  50. Machine* host = super->host;
  51. const Settings* settings = host->settings;
  52. // Finish process table update, culling any exit'd processes
  53. for (int i = Vector_size(super->rows) - 1; i >= 0; i--) {
  54. Process* p = (Process*) Vector_get(super->rows, i);
  55. // tidy up Process state after refreshing the ProcessTable table
  56. Process_makeCommandStr(p, settings);
  57. // keep track of the highest UID for column scaling
  58. if (p->st_uid > host->maxUserId)
  59. host->maxUserId = p->st_uid;
  60. Table_cleanupRow(super, (Row*) p, i);
  61. }
  62. // compact the table in case of deletions
  63. Table_compact(super);
  64. }
  65. const TableClass ProcessTable_class = {
  66. .super = {
  67. .extends = Class(Table),
  68. .delete = ProcessTable_delete,
  69. },
  70. .prepare = ProcessTable_prepareEntries,
  71. .iterate = ProcessTable_iterateEntries,
  72. .cleanup = ProcessTable_cleanupEntries,
  73. };