[9c22e5]: dae / oprofiled.c  Maximize  Restore  History

Download this file

417 lines (348 with data), 11.4 kB

  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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* $Id: oprofiled.c,v 1.21 2000/12/12 02:55:35 moz Exp $ */
/* COPYRIGHT (C) 2000 THE VICTORIA UNIVERSITY OF MANCHESTER and John Levon
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* 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 "oprofiled.h"
extern struct opd_footer footer;
static int showvers;
static u8 ctr0_type_val;
static u8 ctr1_type_val;
/* unit masks used for the samples file output */
static int ctr0_um;
static int ctr1_um;
static int cpu_type;
static int ignore_myself;
static char *ctr0_type="INST_RETIRED";
static char *ctr1_type="";
static int opd_buf_size=OPD_DEFAULT_BUF_SIZE;
static char *opd_dir="/var/opd/";
static char *logfilename="oprofiled.log";
char *smpdir="/var/opd/samples/";
static char *devfilename="opdev";
static char *devmapfilename="opmapdev";
static char *devhashmapfilename="ophashmapdev";
char *vmlinux;
static char *systemmapfilename;
static pid_t mypid;
static sigset_t maskset;
static fd_t devfd;
fd_t mapdevfd;
char *hashmap;
static void opd_sighup(int val);
static void opd_open_logfile(void);
unsigned long opd_stats[OPD_MAX_STATS] = { 0, };
static struct poptOption options[] = {
{ "buffer-size", 'b', POPT_ARG_INT, &opd_buf_size, 0, "nr. of entries in kernel buffer", "num", },
{ "ctr0-unit-mask", 'u', POPT_ARG_INT, &ctr0_um, 0, "unit mask for ctr0", "val", },
{ "ctr1-unit-mask", 't', POPT_ARG_INT, &ctr1_um, 0, "unit mask for ctr1", "val", },
{ "ctr0-event", '0', POPT_ARG_STRING, &ctr0_type, 0, "symbolic event name for ctr0", "name", },
{ "ctr1-event", '1', POPT_ARG_STRING, &ctr1_type, 0, "symbolic event name for ctr1", "name", },
{ "use-cpu", 'p', POPT_ARG_INT, &cpu_type, 0, "0 for PPro, 1 for PII, 2 for PIII", "[0|1|2]" },
{ "ignore-myself", 'm', POPT_ARG_INT, &ignore_myself, 0, "ignore samples of oprofile driver", "[0|1]"},
{ "log-file", 'l', POPT_ARG_STRING, &logfilename, 0, "log file", "file", },
{ "base-dir", 'd', POPT_ARG_STRING, &opd_dir, 0, "base directory of daemon", "dir", },
{ "samples-dir", 's', POPT_ARG_STRING, &smpdir, 0, "output samples dir", "file", },
{ "device-file", 'd', POPT_ARG_STRING, &devfilename, 0, "profile device file", "file", },
{ "map-device-file", 'd', POPT_ARG_STRING, &devmapfilename, 0, "profile mapping device file", "file", },
{ "hash-map-device-file", 'h', POPT_ARG_STRING, &devhashmapfilename, 0, "profile hashmap device file", "file", },
{ "map-file", 'f', POPT_ARG_STRING, &systemmapfilename, 0, "System.map for running kernel file", "file", },
{ "vmlinux", 'k', POPT_ARG_STRING, &vmlinux, 0, "vmlinux kernel image", "file", },
{ "version", 'v', POPT_ARG_NONE, &showvers, 0, "show version", NULL, },
POPT_AUTOHELP
{ NULL, 0, 0, NULL, 0, NULL, NULL, },
};
/**
* opd_open_logfile - open the log file
*
* Open the logfile on stdout and stderr. This function
* assumes that 1 and 2 are the lowest closed()d file
* descriptors. Failure to open on either descriptor is
* a fatal error.
*/
static void opd_open_logfile(void)
{
if (open(logfilename,O_WRONLY|O_CREAT|O_NOCTTY|O_APPEND,0755)==-1) {
perror("oprofiled: couldn't re-open stdout: ");
exit(1);
}
if (dup2(1,2)==-1) {
perror("oprofiled: couldn't dup stdout to stderr: ");
exit(1);
}
}
/**
* opd_open_files - open necessary files
*
* Open the three device files and the log file,
* and mmap() the hash map. Also read the System.map
* file.
*/
static void opd_open_files(void)
{
fd_t hashmapdevfd;
hashmapdevfd = opd_open_device(devhashmapfilename,1);
mapdevfd = opd_open_device(devmapfilename,1);
/* must open last as this enables profiling */
devfd = opd_open_device(devfilename,1);
hashmap = mmap(0, OP_HASH_MAP_SIZE, PROT_READ, MAP_SHARED, hashmapdevfd, 0);
if ((long)hashmap==-1) {
perror("oprofiled: couldn't mmap hash map: ");
exit(1);
}
/* set up logfile */
close(0);
close(1);
if (open("/dev/null",O_RDONLY)==-1) {
perror("oprofiled: couldn't re-open stdin as /dev/null: ");
exit(1);
}
opd_open_logfile();
opd_read_system_map(systemmapfilename);
printf("oprofiled started %s",opd_get_time());
fflush(stdout);
}
/**
* opd_options - parse command line options
* @argc: argc
* @argv: argv array
*
* Parse all command line arguments, and sanity
* check what the user passed. Incorrect arguments
* are a fatal error.
*/
static void opd_options(int argc, char const *argv[])
{
poptContext optcon;
int ret;
char c;
optcon = poptGetContext(NULL, argc, argv, options, 0);
c=poptGetNextOpt(optcon);
if (c<-1) {
fprintf(stderr, "oprofiled: %s: %s\n",
poptBadOption(optcon, POPT_BADOPTION_NOALIAS),
poptStrerror(c));
poptPrintHelp(optcon, stderr, 0);
exit(1);
}
if (showvers) {
printf(VERSION_STRING " compiled on " __DATE__ " " __TIME__ "\n");
exit(0);
}
if (!vmlinux || streq("",vmlinux)) {
fprintf(stderr, "oprofiled: no vmlinux specified.\n");
poptPrintHelp(optcon, stderr, 0);
exit(1);
}
if (!systemmapfilename || streq("",systemmapfilename)) {
fprintf(stderr, "oprofiled: no System.map specified.\n");
poptPrintHelp(optcon, stderr, 0);
exit(1);
}
ret = op_check_events_str(ctr0_type, ctr1_type, (u8)ctr0_um, (u8)ctr1_um, cpu_type, &ctr0_type_val, &ctr1_type_val);
if (ret&OP_CTR0_NOT_FOUND) fprintf(stderr, "oprofiled: ctr0: no such event\n");
if (ret&OP_CTR1_NOT_FOUND) fprintf(stderr, "oprofiled: ctr1: no such event\n");
if (ret&OP_CTR0_NO_UM) fprintf(stderr, "oprofiled: ctr0: invalid unit mask\n");
if (ret&OP_CTR1_NO_UM) fprintf(stderr, "oprofiled: ctr1: invalid unit mask\n");
if (ret&OP_CTR0_NOT_ALLOWED) fprintf(stderr, "oprofiled: ctr0: can't count event\n");
if (ret&OP_CTR1_NOT_ALLOWED) fprintf(stderr, "oprofiled: ctr1: can't count event\n");
if (ret&OP_CTR0_PII_EVENT) fprintf(stderr, "oprofiled: ctr0: event only available on PII\n");
if (ret&OP_CTR1_PII_EVENT) fprintf(stderr, "oprofiled: ctr1: event only available on PII\n");
if (ret&OP_CTR0_PIII_EVENT) fprintf(stderr, "oprofiled: ctr0: event only available on PIII\n");
if (ret&OP_CTR1_PIII_EVENT) fprintf(stderr, "oprofiled: ctr1: event only available on PIII\n");
if (ret!=OP_EVENTS_OK) {
poptPrintHelp(optcon, stderr, 0);
exit(1);
}
}
/**
* opd_fork - fork and return as child
*
* fork() and exit the parent with _exit().
* Failure is fatal.
*/
static void opd_fork(void)
{
switch (fork()) {
case -1:
perror("oprofiled: fork() failed: ");
exit(1);
break;
case 0:
break;
default:
/* parent */
_exit(0);
break;
}
}
/**
* opd_go_daemon - become daemon process
*
* Become an un-attached daemon in the standard
* way (fork(),chdir(),setsid(),fork()). Sets
* the global variable mypid to the pid of the second
* child. Parents perform _exit().
*
* Any failure is fatal.
*/
static void opd_go_daemon(void)
{
opd_fork();
if (chdir(opd_dir)) {
fprintf(stderr,"oprofiled: opd_go_daemon: couldn't chdir to %s: %s",opd_dir,strerror(errno));
exit(1);
}
if (setsid() < 0) {
perror("oprofiled: opd_go_daemon: couldn't setsid: ");
exit(1);
}
opd_fork();
mypid = getpid();
}
void opd_do_samples(const struct op_sample *opd_buf, size_t count);
/**
* opd_do_read - enter processing loop
* @buf: buffer to read into
* @size: size of buffer
*
* Read some of a buffer from the device and process
* the contents.
*
* Never returns.
*/
static void opd_do_read(struct op_sample *buf, size_t size)
{
size_t count;
while (1) {
count=opd_read_device(devfd,buf,size,TRUE);
opd_do_samples(buf,count);
}
}
/**
* opd_is_mapping - is the entry a notification
* @sample: sample to use
*
* Returns positive if the sample is actually a notification,
* zero otherwise.
*/
inline static u16 opd_is_mapping(const struct op_sample *sample)
{
return (sample->count & OP_MAPPING);
}
/**
* opd_do_samples - process a sample buffer
* @opd_buf: buffer to process
* @count: number of bytes in buffer
*
* Process a buffer of samples.
* The signals specified by the global variable maskset are
* masked. Samples for oprofiled are ignored if the global
* variable ignore_myself is set.
*
* If the sample could be processed correctly, it is written
* to the relevant sample file. Additionally mapping and
* process notifications are handled here.
*/
void opd_do_samples(const struct op_sample *opd_buf, size_t count)
{
uint i;
/* prevent signals from messing us up */
sigprocmask(SIG_BLOCK,&maskset,NULL);
opd_stats[OPD_DUMP_COUNT]++;
for (i=0; i < count/sizeof(struct op_sample); i++) {
dprintf("%.6u: EIP: 0x%.8x pid: %.6d count: %.6d\n", i, opd_buf[i].eip, opd_buf[i].pid, opd_buf[i].count);
if (ignore_myself && opd_buf[i].pid==mypid)
continue;
if (opd_is_mapping(&opd_buf[i])) {
opd_stats[OPD_NOTIFICATIONS]++;
switch (opd_buf[i].count) {
case OP_FORK:
opd_handle_fork(&opd_buf[i]);
break;
case OP_EXEC:
opd_handle_exec(&opd_buf[i]);
break;
case OP_MAP:
opd_handle_mapping(&opd_buf[i]);
break;
case OP_DROP_MODULES:
opd_clear_module_info();
break;
case OP_EXIT:
opd_handle_exit(&opd_buf[i]);
break;
default:
fprintf(stderr, "Received unknown notification type %u\n",opd_buf[i].count);
exit(1);
break;
}
} else
opd_put_sample(&opd_buf[i]);
}
sigprocmask(SIG_UNBLOCK,&maskset,NULL);
}
/* re-open logfile for logrotate */
static void opd_sighup(int val __attribute__((unused)))
{
close(1);
close(2);
opd_open_logfile();
}
int main(int argc, char const *argv[])
{
struct op_sample *opd_buf;
size_t opd_buf_bytesize;
struct sigaction act;
int i;
opd_options(argc, argv);
footer.ctr0_type_val = ctr0_type_val;
footer.ctr0_um = (u8)ctr0_um;
footer.ctr1_type_val = ctr1_type_val;
footer.ctr1_um = (u8)ctr1_um;
/* one extra for the "how many" header */
opd_buf_bytesize=(opd_buf_size+1)*sizeof(struct op_sample);
opd_buf = opd_malloc(opd_buf_bytesize);
opd_init_images();
/* yes, this is racey. */
opd_get_ascii_procs();
opd_go_daemon();
opd_open_files();
for (i=0; i< OPD_MAX_STATS; i++) {
opd_stats[i] = 0;
}
act.sa_handler=opd_alarm;
act.sa_flags=0;
sigemptyset(&act.sa_mask);
if (sigaction(SIGALRM, &act, NULL)) {
perror("oprofiled: install of SIGALRM handler failed: ");
exit(1);
}
act.sa_handler=opd_sighup;
act.sa_flags=0;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask,SIGALRM);
if (sigaction(SIGHUP, &act, NULL)) {
perror("oprofiled: install of SIGHUP handler failed: ");
exit(1);
}
sigemptyset(&maskset);
sigaddset(&maskset,SIGALRM);
sigaddset(&maskset,SIGHUP);
/* clean up every 10 minutes */
alarm(60*10);
/* simple sleep-then-process loop */
opd_do_read(opd_buf,opd_buf_bytesize);
return 0;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks