File: ProcessTable.c

package info (click to toggle)
pcp 7.1.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 252,748 kB
  • sloc: ansic: 1,483,656; sh: 182,366; xml: 160,462; cpp: 83,813; python: 24,980; perl: 18,327; yacc: 6,877; lex: 2,864; makefile: 2,738; awk: 165; fortran: 60; java: 52
file content (102 lines) | stat: -rw-r--r-- 2,842 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
htop - ProcessTable.c
(C) 2004,2005 Hisham H. Muhammad
Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/

#include "config.h" // IWYU pragma: keep

#include "ProcessTable.h"

#include <assert.h>
#include <stdlib.h>

#include "Hashtable.h"
#include "Row.h"
#include "Settings.h"
#include "Vector.h"


void ProcessTable_init(ProcessTable* this, const ObjectClass* klass, Machine* host, Hashtable* pidMatchList) {
   Table_init(&this->super, klass, host);

   this->pidMatchList = pidMatchList;
}

void ProcessTable_done(ProcessTable* this) {
   Table_done(&this->super);
}

Process* ProcessTable_getProcess(ProcessTable* this, pid_t pid, bool* preExisting, Process_New constructor) {
   const Table* table = &this->super;
   Process* proc = (Process*) Hashtable_get(table->table, pid);
   *preExisting = proc != NULL;
   if (proc) {
      assert(Vector_indexOf(table->rows, proc, Row_idEqualCompare) != -1);
      assert(Process_getPid(proc) == pid);
   } else {
      proc = constructor(table->host);
      assert(proc->cmdline == NULL);
      Process_setPid(proc, pid);
   }
   return proc;
}

static void ProcessTable_prepareEntries(Table* super) {
   ProcessTable* this = (ProcessTable*) super;
   this->totalTasks = 0;
   this->userlandThreads = 0;
   this->kernelThreads = 0;
   this->runningTasks = 0;

   Table_prepareEntries(super);
}

static void ProcessTable_iterateEntries(Table* super) {
   ProcessTable* this = (ProcessTable*) super;
   // calling into platform-specific code
   ProcessTable_goThroughEntries(this);
}

static void ProcessTable_cleanupEntries(Table* super) {
   Machine* host = super->host;
   const Settings* settings = host->settings;

   // Lowest index of the row that is soft-removed. Used to speed up
   // compaction.
   int dirtyIndex = Vector_size(super->rows);

   // Finish process table update, culling any exit'd processes
   for (int i = Vector_size(super->rows) - 1; i >= 0; i--) {
      Process* p = (Process*) Vector_get(super->rows, i);

      // tidy up Process state after refreshing the ProcessTable table
      Process_makeCommandStr(p, settings);

      // keep track of the highest UID and PID for column scaling
      if (p->st_uid > host->maxUserId)
         host->maxUserId = p->st_uid;

      pid_t pid = Process_getPid(p);
      if (pid > host->maxProcessId)
         host->maxProcessId = pid;

      if (!Table_cleanupRow(super, &p->super, i)) {
         dirtyIndex = i;
      }
   }

   // compact the table in case of deletions
   Table_compact(super, dirtyIndex);
}

const TableClass ProcessTable_class = {
   .super = {
      .extends = Class(Table),
      .delete = ProcessTable_delete,
   },
   .prepare = ProcessTable_prepareEntries,
   .iterate = ProcessTable_iterateEntries,
   .cleanup = ProcessTable_cleanupEntries,
};