summaryrefslogtreecommitdiffstats
path: root/sys-utils/readprofile.c
blob: 58234f6af76766437f55ea2e513e9115414db30b (plain) (blame)
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
/*
 *  readprofile.c - used to read /proc/profile
 *
 *  Copyright (C) 1994 Alessandro Rubini
 *
 *   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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>  /* getopt() */
#include <string.h>

#define RELEASE "1.1, Jan 1995"

#define S_LEN 128

static char *prgname;

/* These are the defaults and they cna be changed */
static char defaultmap1[]="/usr/src/linux/System.map";
static char defaultmap2[]="/usr/src/linux/zSystem.map";
static char defaultpro[]="/proc/profile";
static char optstring[]="m:p:itvarV";

void usage()
{
  fprintf(stderr,
		  "%s: Usage: \"%s [options]\n"
		  "\t -m <mapfile>  (default = \"%s\")\n"
		  "\t -p <pro-file> (default = \"%s\")\n"
		  "\t -i            print only info about the sampling step\n"
		  "\t -t            print terse data\n"
		  "\t -v            print verbose data\n"
		  "\t -a            print all symbols, even if count is 0\n"
		  "\t -r            reset all the counters (root only)\n"
		  "\t -V            print version and exit\n"
		  ,prgname,prgname,defaultmap1,defaultpro);
  exit(1);
}

FILE *myopen(char *name, char *mode, int *flag)
{
static char cmdline[S_LEN];

  if (!strcmp(name+strlen(name)-3,".gz"))
	{
	*flag=1;
	sprintf(cmdline,"zcat %s", name);
	return popen(cmdline,mode);
	}
  *flag=0;
  return fopen(name,mode);
}

int main (int argc, char **argv)
{
FILE *pro;
FILE *map;
unsigned long l;
char *proFile;
char *mapFile;
int add, step;
int fn_add[2];            /* current and next address */
char fn_name[2][S_LEN];   /* current and next name */
char mode[8];
int i,c,current=0;
int optAll=0, optInfo=0, optReset=0, optTerse=0, optVerbose=0;
char mapline[S_LEN];
int maplineno=1;
int popenMap, popenPro;   /* flags to tell if popen() is used */

#define next (current^1)

  prgname=argv[0];
  proFile=defaultpro;
  mapFile=defaultmap1;

  while ((c=getopt(argc,argv,optstring))!=-1)
    {
    switch(c)
      {
      case 'm': mapFile=optarg; break;
      case 'p': proFile=optarg; break;
      case 'a': optAll++;       break;
      case 'i': optInfo++;      break;
	  case 't': optTerse++;     break;
	  case 'r': optReset++;     break;
	  case 'v': optVerbose++;   break;
	  case 'V': printf("%s Version %s\n",prgname,RELEASE); exit(0);
      default: usage();
      }
    }

  if (optReset)
	{
	pro=fopen(defaultpro,"w");
	if (!pro)
	  {perror(proFile); exit(1);}
	fprintf(pro,"anything\n");
	fclose(pro);
    exit(0);
	}

  if (!(pro=myopen(proFile,"r",&popenPro))) 
    {fprintf(stderr,"%s: ",prgname);perror(proFile);exit(1);}

  /*
   * In opening the map file, try both the default names, but exit
   * at first fail if the filename was specified on cmdline
   */
  for (map=NULL; map==NULL; )
	{
	if (!(map=myopen(mapFile,"r",&popenMap)))
	  {
	  fprintf(stderr,"%s: ",prgname);perror(mapFile);
	  if (mapFile!=defaultmap1) exit(1);
	  mapFile=defaultmap2;
	  }
	}

#define NEXT_WORD(where) \
        (fread((void *)where, 1,sizeof(unsigned long),pro), feof(pro) ? 0 : 1)

  /*
   * Init the 'next' field
   */
  if (!fgets(mapline,S_LEN,map))
	{
	fprintf(stderr,"%s: %s(%i): premature EOF\n",prgname,mapFile,maplineno);
	exit(1);
	}
  if (sscanf(mapline,"%x %s %s",&(fn_add[next]),mode,fn_name[next])!=3)
	{
	fprintf(stderr,"%s: %s(%i): wrong map line\n",prgname,mapFile, maplineno);
	exit(1);
	}

  add=0;

  if (!NEXT_WORD(&step))
	{
    fprintf(stderr,"%s: %s: premature EOF\n",prgname,proFile);
    exit(1);
    }

  if (optInfo)
    {
    printf(optTerse ? "%i\n" : "The sampling step in the kernel is %i bytes\n",
		   step);
    exit(0);
    } 

  /*
   * The main loop is build around the mapfile
   */
  
  while(current^=1, maplineno++, fgets(mapline,S_LEN,map))
    {
    int fn_len;
    int count=0;


	if (sscanf(mapline,"%x %s %s",&(fn_add[next]),mode,fn_name[next])!=3)
	  {
	  fprintf(stderr,"%s: %s(%i): wrong map line\n",
			  prgname,mapFile, maplineno);
	  exit(1);
	  }

	if (!(fn_len=fn_add[next]-fn_add[current]))
	  continue;

    if (*mode=='d' || *mode=='D') break; /* only text is profiled */

    while (add<fn_add[next])
      {
      if (!NEXT_WORD(&l))
		{
		fprintf(stderr,"%s: %s: premature EOF\n",prgname,proFile);
		exit(1);
		}
      count+=l; add+=step;
      }

    if (count || optAll)
	  {
	  if (optTerse)
		printf("%i %s %lg\n",
			   count,fn_name[current],count/(double)fn_len);
	  else if (optVerbose)
		printf("%08x %-40s %6i %8.4lf\n",
			   fn_add[current],fn_name[current],count,count/(double)fn_len);
	  else
		printf("%6i %-40s %8.4lf\n",
			   count,fn_name[current],count/(double)fn_len);
	  }
	}

  if (feof(map))
	{
	fprintf(stderr,"%s: %s(%i): premature EOF\n",prgname,mapFile,maplineno);
	exit(1);
	}
	
  popenPro ? pclose(pro) : fclose(pro);
  popenMap ? pclose(map) : fclose(map);
  exit(0);
}