[go: up one dir, main page]

File: fkt_record.c

package info (click to toggle)
fxt 0.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,208 kB
  • ctags: 1,970
  • sloc: sh: 11,340; ansic: 7,915; perl: 3,268; asm: 598; makefile: 202
file content (521 lines) | stat: -rw-r--r-- 12,843 bytes parent folder | download | duplicates (6)
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*	fkt_record.c

	fkt_record --	control program to allow a user to trace his/her programs
					with fkt (fast kernel tracing) in the kernel.

	Copyright (C) 2000, 2001  Robert D. Russell -- rdr@unh.edu
	2003, 2004, 2012  Samuel Thibault -- samuel.thibault@ens-lyon.org

	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 <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/wait.h>
#include <sys/times.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/sendfile.h>
#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
#include <sys/vfs.h>
#include <sys/mman.h>
#include <sys/utsname.h>
#include <fcntl.h>
#include <signal.h>
#include <linux/fkt.h>
#include "timer.h"
#include "fxt.h"

#include "fxt-tools.h"

#ifndef WHITE_SPACE
#define WHITE_SPACE	" \t\n\f\v\r"
#endif

#define OPTIONS ":f:k:s:p:dhnS:"
#define FKT_DEV "/dev/fkt"
#define MAXCPUS	16


static int fd,fkt;
static int kpid = -1;
static size_t size = 0;

struct code_list_item	{
	unsigned int			code;
	char					*name;
	struct code_list_item	*link;
};

void dumptime( char *message , time_t *the_time, clock_t *the_jiffies )
	{
	struct tms		cur_time;
	struct tm		*breakout;
	char			buffer[128];

	if( (*the_time = time(NULL)) == -1 )
		perror("time");

	if( (*the_jiffies = times(&cur_time)) == (clock_t) -1 )
		perror("times");

	if( (breakout = localtime(the_time)) == NULL )
		fprintf(stderr, "Unable to break out current time\n");

	if( strftime(buffer, 128, "%d-%b-%Y %H:%M:%S", breakout) == 0 )
		fprintf(stderr, "Unable to convert current time\n");

	printf("%7s: %s: user_time = %ld, system_time = %ld, jiffies = %ld\n",
			message, buffer,
			(long)cur_time.tms_utime, (long)cur_time.tms_stime,
			(long)*the_jiffies);
	fflush(stdout);
	}


double pr_times( FILE * fp, clock_t real, struct tms * tmsstart,
											struct tms * tmsend, int verbose )
	{
    static long clktck;
	double	elapsed;

    if (clktck == 0)
        if((clktck = sysconf(_SC_CLK_TCK)) < 0)
            perror("sysconf error");

	elapsed = ((double)real)/((double)clktck);
	if( verbose )
		{
		fprintf(fp, "Elapsed: %7.2f, ", elapsed);
		fprintf(fp, "user: %7.2f, ", (tmsend->tms_utime - tmsstart->tms_utime) /
			   (double) clktck);
		fprintf(fp, "sys:  %7.2f\n", (tmsend->tms_stime - tmsstart->tms_stime) /
			   (double) clktck);
		}
	return elapsed;
	}

/*	this is the stuff that will be traced if user doesn't supply anything else*/
pid_t do_some_stuff( void )
	{
	pid_t		kidpid;
	int			i, j, sum;
	struct tms	tmsstart, tmsstop;
	clock_t		start, stop;

	fflush(stdout);
	if( (kidpid = fork()) < 0 )
		{
		perror("fork");
		sleep(4);
		}
	else if( kidpid == 0 )
		{/* this is the kid */
		printf("job with pid %d started\n", (int)getpid());
		fflush(stdout);
		ioctl(fkt,FKT_WAITREADY);	/* wait for sendfile to settle down */
		if( (start = times(&tmsstart)) < 0 )
			exit(EXIT_FAILURE);
		for( i = 0;  i < 4;  i++ )
			{
			sum = 0;
			for( j = 0;  j < 1000000;  j++ )
				sum += j;
			printf("new kid: i = %d, j = %d\n", i, j);
			fflush(stdout);
			sleep(1);
			}
		if( (stop = times(&tmsstop)) < 0 )
			exit(EXIT_FAILURE);
		printf("job with pid %d finished\n", (int)getpid());
		fflush(stdout);
		pr_times(stdout, stop-start, &tmsstart, &tmsstop, 1);
		exit(EXIT_SUCCESS);
		}
	/*	this is the parent, just return the kid's pid */
	kpid = kidpid;
	return kidpid;
	}


/* this creates a job requested by user and traces that */
pid_t spawn_stuff( int argc, char *argv[] )
	{
	pid_t	kidpid;

	fflush(stdout);
	if( (kidpid = fork()) < 0 )
		{
		perror("fork");
		}
	else if( kidpid == 0 )
		{/* this is the kid */
			struct tms tms;
		ioctl(fkt,FKT_WAITREADY);	/* wait for sendfile to settle down */
		if (times(&tms)<0) {
			perror("times");
			exit(EXIT_FAILURE);
		}
		printf("executing %s\n",argv[optind]);
		execvp(argv[optind], &argv[optind]);
		perror(argv[optind]);
		exit(EXIT_FAILURE);
		}
	/*	this is the parent, just return the kid's pid */
	kpid = kidpid;
	return kidpid;
	}

/* handler for SIGINT */
void int_handler(int signo)
	{
	
	  printf("\n PARENT HANDLER");
	  fflush(stdout);
	  if(kpid != -1 && kill(kpid, SIGINT) == -1)
		{
		   perror("\nError in kill");
		}
	}

/* handler for child death, just to have sendfile interrupted */
void chld_handler(int signo)
{
	printf("\n Child died");
	fflush(stdout);
}

void usage(char *progname) {
	printf("\
Usage: %s [-k mask] [-f output-file] [-s size] [-S System.map] [-p pow]\n\
           [--] [program p1 p2 p3 ...]\n\
\n\
	if -f is not given, look for TRACE_FILE environment variable, and\n\
	if that is not given, write to \"trace_file\".\n\
\n\
	if -k is given, use the indicated mask to enable tracing.\n\
	if that is not given, use mask of 1 (only syscalls, irqs and traps).\n\
\n\
	if -s is not given or set to 0, the filesystem's size is used.\n\
	(K, M and G suffixes are accepted)\n\
\n\
	if -S is given, the given System.map is merged with /proc/kallsyms or\n\
	/proc/ksyms and recorded before the actual recording. An error is raised\n\
	if an inconsistency is detected (i.e. this System.map does not\n\
	correspond to the current kernel.\n\
\n\
	if -n is given, no program is launched at all, you have to press ^C to\n\
	interrupt recording\n\
\n\
	if -p is given, fkt will allocate 2^pow pages for its recording buffer\n\
	it is useful if load pikes are expected during the recording.\n\
	the default is 7.\n\
\n\
	if program is not given, just spawn a nonsense kid to burn cpu.\n\
",progname);
}

int main( int argc, char *argv[] )
	{
	pid_t				kidpid = 0, donepid = 0;
	int					status = 0;
	int					c, i;
	char				*outfile, *ptr;
	unsigned int		keymask;
	int					powpages=0,dma=0,nop=0;
	off_t				dummyoff=0;
	struct sigaction  	sig_act1;
	struct statfs		statfs;
	struct stat			st;
	ssize_t				ret;
	fxt_t				fxt;
	struct fxt_infos	*info;

	sig_act1.sa_handler = int_handler;
	sigemptyset(&sig_act1.sa_mask);
	sig_act1.sa_flags = 0;
	sigaddset(&sig_act1.sa_mask, SIGINT);

	outfile = NULL;
	keymask = 1;
	opterr = 0;

	if (!(fxt = fxt_setinfos(FXT_SPACE_KERNEL)))
		{
		perror("allocating fxt record");
		exit(EXIT_FAILURE);
		}

	while( (c = getopt(argc, argv, OPTIONS)) != EOF )
		switch( c )
			{
		case 'k':
			keymask = strtoll(optarg, &ptr, 0);
			if( strspn(ptr, WHITE_SPACE) != strlen(ptr) )
				{
				fprintf(stderr, "illegal keymask parameter %s\n", optarg);
				exit(EXIT_FAILURE);
				}
			break;
		case 'f':
			outfile = optarg;
			break;
		case 's': {
			size_t mult = 1;
			switch (optarg[strlen(optarg)-1]) {
			case 'K': case 'k': mult = 1 << 10; break;
			case 'M': case 'm': mult = 1 << 20; break;
			case 'G': case 'g': mult = 1 << 30; break;
			}
			if (mult>1) optarg[strlen(optarg)-1]='\0';
			size = strtol(optarg, &ptr, 0) * mult;
			if( strspn(ptr, WHITE_SPACE) != strlen(ptr) )
				{
				fprintf(stderr, "illegal size parameter %s\n", optarg);
				exit(EXIT_FAILURE);
				}
			break;
		}
		case 'p':
			powpages = strtol(optarg, &ptr, 0);
			if( strspn(ptr, WHITE_SPACE) != strlen(ptr) )
				{
				fprintf(stderr, "illegal power parameter %s\n", optarg);
				exit(EXIT_FAILURE);
				}
			break;
		case 'd':
			dma = 1;
			break;
		case 'n':
			nop = 1;
			break;
		case 'S':
			fxt_get_symbols(fxt,optarg,FXT_SYSMAP_FILE,0);
			break;
		case ':':
			fprintf(stderr, "missing parameter to switch %c\n", optopt);
			usage(argv[0]);
			exit(EXIT_FAILURE);
		case 'h':
			usage(argv[0]);
			exit(0);
		case '?':
			fprintf(stderr, "illegal switch %c\n", optopt);
			usage(argv[0]);
			exit(EXIT_FAILURE);
			}	/* switch */

	/*	get the cpu info here */
	info = fxt_infos(fxt);
	printf("%7s: %d\n", "n_cpus", info->ncpus);
	for( i = 0;  i < info->ncpus;  i++ )
		printf("  cpu %d: %d MHZ\n", i, info->mhz[i]);


	printf("%7s: %d\n", "pid", info->record_pid);

	if( outfile == NULL )
		{
		if( (outfile = getenv("TRACE_FILE")) == NULL )
			outfile = DEFAULT_TRACE_FILE;
		}
	while( (fd = open(outfile, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0 )
		{/* could not open the indicated file name for writing */
		perror(outfile);
		if( strcmp(outfile, DEFAULT_TRACE_FILE) == 0 )
			/* can't open the default file, have to give up */
			exit(EXIT_FAILURE);
		outfile = DEFAULT_TRACE_FILE;
		fprintf(stderr, "output defaulting to %s\n", outfile);
		}

	if( size == 0 )
		{
		if( fstat(fd, &st) )
			{
			perror("getting attributes of trace file");
			exit(EXIT_FAILURE);
			}
#ifdef BLKGETSIZE
		if( S_ISBLK(st.st_mode) )
			{
			int sectors;
			printf("block device\n");
			if( ioctl(fd,BLKGETSIZE,&sectors) )
				{
				perror("getting device size\n");
				exit(EXIT_FAILURE);
				}
			if( sectors >= 0xffffffffUL >> 9 )
				size=0xffffffffUL;
			else
				size = sectors << 9;
			}
		else 
#endif
			{
			if( fstatfs(fd, &statfs) )
				{
				perror("getting file system size\n");
				exit(EXIT_FAILURE);
				}
			if( statfs.f_bavail >= 0xffffffffUL / statfs.f_bsize )
				size = 0xffffffffUL;
			else
				size = statfs.f_bsize * statfs.f_bavail;
			}
		printf("%zuM\n",size>>20);
		}

	/*	force all I/O to be synchronized at this point */
	for( i = 0;  i <= fd;  i++ )
		fsync(i);

	if( (fkt = open(FKT_DEV, O_RDONLY|O_NONBLOCK)) < 0 ) {
		perror("open(\"" FKT_DEV "\")");
		exit(EXIT_FAILURE);
	}

	if( ioctl(fkt, FKT_SETINITMASK, keymask) < 0 ) {
		perror("setting initial mask");
		exit(EXIT_FAILURE);
	}

	if( powpages )
	if( ioctl(fkt, FKT_SETINITPOWPAGES, powpages) < 0 ) {
		perror("setting initial number of pages");
		exit(EXIT_FAILURE);
	}

	if( dma )
	if( ioctl(fkt, FKT_SETTRYDMA, dma) < 0 )
		perror("asking for trying to get dma");

	if(sigaction(SIGINT, &sig_act1, NULL))
		{
		  perror("\nsigaction SIGINT\n");
		}

	signal(SIGCHLD,chld_handler);

	if( !nop ) {
		if( optind < argc )
			kidpid = spawn_stuff(argc, argv);
		else
			kidpid = do_some_stuff();
	}

	/* record information as soon as now: in case we crash,
	 * only the stop_time & jiffies will be wrong (doesn't hurt that much
	 * for the analysis) */
	dumptime("start", &info->start_time, &info->start_jiffies);

	/* merge of System.map and /proc/ksyms */
	if( !stat("/proc/kallsyms",&st) )
		{/* on >=2.5 kernels, kallsyms gives everything we may want */
		fxt_get_symbols(fxt,"/proc/kallsyms",FXT_MODLIST,0);
		fxt_get_symbols(fxt,"/proc/kallsyms",FXT_PROC,0);
		}
	else
		{/* but on 2.4 kernels, only ksyms is available */
		fxt_get_symbols(fxt,"/proc/ksyms",FXT_MODLIST,0);
		fxt_get_symbols(fxt,"/proc/ksyms",FXT_PROC,0);
		}

	if (fxt_fdwrite(fxt,fd)<0)
		{
		perror("fxt_fdwrite");
		exit(EXIT_FAILURE);
		}
	if (fxt_fdevents_start(fxt,fd,FXT_TRACE_KERNEL_RAW)<0)
		{
		perror("fxt_fdstartevents");
		exit(EXIT_FAILURE);
		}

	/* ok, now we've consumed our quota, wait for a second to get back some
	 * priority for the recording. */

	sleep(1);
	printf("launching recording\n");
	ret = sendfile(fd, fkt, &dummyoff, size);
	if( ret < 0 )
		{
		perror("sendfile");
		fprintf(stderr,"hoping the trace wasn't lost\n");
		}

	dumptime("stop", &info->stop_time, &info->stop_jiffies);

	if( kidpid > 0 )
	{/* control came back with kidpid>0, child started ok, wait for it */
		if (!(donepid = waitpid(kidpid, &status, WNOHANG))) {

	/* kill kid if not dead already, since recording ended */
		  if(kill(kidpid, SIGINT) == -1)
			{
			   perror("\nError in kill");
			}

		  donepid = waitpid(kidpid, &status, 0);
		}
		/* print out stuff about the kid */
		kpid = kidpid;
		printf("job with pid %d started\n", (int)kidpid);
		if( donepid < 0 )
			perror("waitpid");
		else if( WIFEXITED(status) )
			printf("job with pid %d exited with status %d\n",
					(int)kidpid, WEXITSTATUS(status));
		else if( WIFSIGNALED(status) )
			printf("job with pid %d killed by signal %d\n",
					(int)kidpid, WTERMSIG(status));
		else
			printf("job with pid %d terminated for unknown reason\n",
					(int)kidpid);
		fflush(stdout);
	}

	if (fxt_fdevents_stop(fxt,fd)<0)
		{
		perror("fxt_fdstartevents");
		exit(EXIT_FAILURE);
		}

	if ( lseek(fd, 0, SEEK_SET) < 0 ) {
		perror("seeking back to the beginning");
		exit(EXIT_FAILURE);
	}
	if (fxt_fdwritetime(fxt, fd)<0)
		{
		perror("fxt_fdwritetime");
		exit(EXIT_FAILURE);
		}
	return 0;
	}

/* vim: ts=4
 */