File: process_list.cc

package info (click to toggle)
lavaps 1.9-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 692 kB
  • ctags: 698
  • sloc: ansic: 2,390; cpp: 2,089; sh: 1,993; tcl: 542; makefile: 229; perl: 182
file content (159 lines) | stat: -rw-r--r-- 3,756 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

/*
 * process_list.cc
 * Copyright (C) 1999 by John Heidemann
 * $Id: process_list.cc,v 1.23 1999/09/14 05:28:17 johnh Exp $
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include <stdlib.h>  // atoi
#include <iostream.h>

#include <unistd.h>
#include <time.h>

#include <stl.h>

#include "main.hh"
#include "process_list.hh"
#include "process_scan.hh"


#if 0
#define PL_DEBUG(x) x
#else /* ! 0 */
#define PL_DEBUG(x) 
#endif /* 0 */




// who'd'a thunk it:
// (sigh...egcs-2.90.29 won't let me make this local to process_list::reset_founds()
struct reset_found_f : public unary_function<pair<const pid_t,process_model*>,void> {
	void operator()(pair<const pid_t,process_model*> p) { p.second->found_ = false; }
};

void
process_list::reset_founds()
{
	ENTRY_TRACE(__FILE__,__LINE__);
	for_each(procs_.begin(), procs_.end(), reset_found_f());
}


struct reap_unfound_f : public unary_function<pair<const pid_t,process_model*>,void> {
	reap_unfound_f(process_list *pl) : pl_(pl) {}
	void operator()(pair<const pid_t,process_model*> p) { if (!p.second->found_) pl_->death(p.second); }
	process_list *pl_;
};

void
process_list::reap_unfounds()
{
	ENTRY_TRACE(__FILE__,__LINE__);
	for_each(procs_.begin(), procs_.end(), reap_unfound_f(this));
}


void
process_list::birth(process_model *pm, time_t now)
{
	ENTRY_TRACE(__FILE__,__LINE__);
	// assert( procs_[pid] doesn't exist)
	procs_[pm->pid()] = pm;
	common(pm, now);
	pm->init_view();
	PL_DEBUG(cout << pm->pid() << " birth\n");
}

void
process_list::life(process_model *pm, time_t now)
{
	ENTRY_TRACE(__FILE__,__LINE__);
	common(pm, now);
	pm->update_view();
}

void
process_list::common(process_model *pm, time_t now)
{
	ENTRY_TRACE(__FILE__,__LINE__);
	pm->found_ = true;
	pm->age(now);
	pm->check_inmem_pct();
}

void
process_list::death(process_model *pm)
{
	ENTRY_TRACE(__FILE__,__LINE__);
	PL_DEBUG(cout << pm->pid() << " death\n");
	procs_.erase(pm->pid());
	delete pm;
}


struct dump_f : public unary_function<pair<const pid_t,process_model*>,void> {
	void operator()(pair<const pid_t,process_model*> p) {
		p.second->dump();
	}
};

void
process_list::dump()
{
	ENTRY_TRACE(__FILE__,__LINE__);
	for_each(procs_.begin(), procs_.end(), dump_f());
}


void
process_list::scan()
{
	ENTRY_TRACE(__FILE__,__LINE__);

	process_scan *scanner = process_scan::open_platform();

	time_t now = time(NULL);
	reset_founds();
	while (scanner->next()) {
		if (filter_style == FILTER_BY_PID && scanner->cur_pid() != filter_good_pid)  // for debugging
			continue;
		if (filter_style == FILTER_BY_UID && scanner->cur_uid() != filter_good_uid)
			continue;
		process_list_map_t::iterator pi = procs_.find(scanner->cur_pid());
		if (pi != procs_.end()) {
			process_model *pm = pi->second;
			scanner->life(pm);
			pm->found_ = true;
			life(pm, now);   // do generic stuff
		} else {
			process_model *pm = scanner->birth();
			pm->found_ = true;
			birth(pm, now);
		};
	};
	delete scanner;
	reap_unfounds();

	// If we intended to switch real<=>virtual mem,
	// we've now done so, so forget about the change.
	report_vm.clear_change();

	// autosize, if necessary
	process_view::autosize();
}