summaryrefslogtreecommitdiff
path: root/src/proc-scanner.c
blob: 336a23f37410faa05490256488610dfbc79a9199 (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Flora License, Version 1.1 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>

#include "proc-scanner.h"
#include "procfs.h"
#include "process.h"
#include "err-check.h"
#include "clock.h"

#define SWAP(a, b, type) do { type tmp = (a); (a) = (b); (b) = tmp; } while(0);
#define PROCTAB_INITIAL_LEN 64

struct proctab {
	struct process *processes;
	int size;
	int max;
};

struct proc_scanner {
	struct proctab current;
	struct proctab history;
	int last_history_index;
};

void proctab_free(struct proctab *tab)
{
	if (!tab) return;
	for (int i = 0; i < tab->size; i++)
		process_shutdown(&tab->processes[i]);
	free(tab->processes);
}

void proctab_resize(struct proctab *tab, int new_max)
{
	tab->processes = realloc(tab->processes, new_max * sizeof(struct process));
	if (!tab->processes) abort();
	tab->max = new_max;
}

void proctab_move_back(struct proctab *tab, struct process *proc)
{
	if (tab->size >= tab->max)
		proctab_resize(tab, tab->max *= 2);
	process_move(&tab->processes[tab->size++],proc);
}

void proctab_reset(struct proctab *tab)
{
	for (int i = 0; i < tab->size; i++)
		process_shutdown(&tab->processes[i]);
	tab->size = 0;
}

proc_scanner_t *proc_scanner_new()
{
	proc_scanner_t *ret = calloc(1, sizeof(proc_scanner_t));
	if (!ret) {
		return NULL;
	}
	proctab_resize(&ret->current, PROCTAB_INITIAL_LEN);
	proctab_resize(&ret->history, PROCTAB_INITIAL_LEN);
	return ret;
}

void proc_scanner_free(proc_scanner_t *scanner)
{
	if (!scanner) return;
	proctab_free(&scanner->current);
	proctab_free(&scanner->history);
	free(scanner);
}

static int _sort_processes_by_pid(const void *a, const void *b)
{
	const struct process *proc1 = a;
	const struct process *proc2 = b;
	return process_get_pid(proc1) - process_get_pid(proc2);
}

static int _search_for_pid_key(const void *a, const void *b)
{
	const int *key = a;
	const struct process *proc = b;
	return *key - proc->pid;
}

static int _sort_pids(const void *a, const void *b)
{
	const int *pid1 = a;
	const int *pid2 = a;
	return pid1 - pid2;
}

// Search for process entry in history
static struct process *_proc_scanner_find_process_in_history(proc_scanner_t *scanner, int pid)
{
	if (scanner->history.size == 0)
		return NULL;

	// the scanner->history_last_history_index helps for quickly find
	// history entry matching given pid. We assume that history is sorted ascending by pid
	// and searching for pid is done in same order.
	// the speedup gain is about ~15% compared to bsearch only.
	while (scanner->last_history_index < scanner->history.size)
	{
		if (scanner->history.processes[scanner->last_history_index].pid < pid)
		{
			scanner->last_history_index++;
			continue;
		}
		if (scanner->history.processes[scanner->last_history_index].pid == pid) {
			return &scanner->history.processes[scanner->last_history_index++];
		}
		break;
	}

	return bsearch(&pid, scanner->history.processes, scanner->history.size, sizeof(struct process), _search_for_pid_key);
}

static bool _proc_scanner_read_pid(int pid, void *user_data)
{
	proc_scanner_t *scanner = user_data;
	struct process proc_new = {0,};

	struct process *proc = _proc_scanner_find_process_in_history(scanner, pid);

	if (!proc) {
		process_init_process(pid, &proc_new);
		proc = &proc_new;
	}

	if (process_update(proc) != 0) {
		process_shutdown(proc);
		return true;
	}
	proctab_move_back(&scanner->current, proc);

	return true;
}

static int _proc_scanner_iterate_array(int *pids, int n_pids, procfs_pid_iterator_cb cb, void *user_data)
{
	qsort(pids, n_pids, sizeof(int), _sort_pids);

	for (int i = 0; i < n_pids; ++i) {
		if (!cb(pids[i], user_data))
			break;
	}
	return 0;
}

static int _proc_scanner_scan_internal(proc_scanner_t *scanner, int *pids, int n_pids)
{
	ON_NULL_RETURN_VAL(scanner, -1);

	// swap processes and history
	SWAP(scanner->current, scanner->history, struct proctab);

	// reset current tab, so we can start adding results of current scan
	proctab_reset(&scanner->current);

	// prepare history
	scanner->last_history_index = 0;
	qsort(scanner->history.processes, scanner->history.size, sizeof(struct process), _sort_processes_by_pid);

	if (pids)
		return _proc_scanner_iterate_array(pids, n_pids, _proc_scanner_read_pid, scanner);
	else
		return procfs_iterate_pids(_proc_scanner_read_pid, scanner);
}

int proc_scanner_scan(proc_scanner_t *scanner)
{
	return _proc_scanner_scan_internal(scanner, NULL, -1);
}
int proc_scanner_scan_pids(proc_scanner_t *scanner, int *pids, int n_pids)
{
	return _proc_scanner_scan_internal(scanner, pids, n_pids);
}

int proc_scanner_foreach_process(proc_scanner_t *scanner, foreach_t cb, void *data)
{
	ON_NULL_RETURN_VAL(scanner, -1);
	ON_NULL_RETURN_VAL(cb, -1);

	for (int i = 0; i < scanner->current.size; ++i) {
		if (!cb(&scanner->current.processes[i], data))
			break;
	}
	return 0;
}

int proc_scanner_sort_processes(proc_scanner_t *scanner, sort_t cb)
{
	ON_NULL_RETURN_VAL(scanner, -1);
	ON_NULL_RETURN_VAL(cb, -1);

	qsort(scanner->current.processes, scanner->current.size, sizeof(struct process), (__compar_fn_t)cb);

	return 0;
}