xref: /plan9/sys/src/cmd/5i/stats.c (revision e288d156a88911460b465926f0fb6de139f6d766)
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <mach.h>
5 #include "arm.h"
6 
7 #define Percent(num, max)	((max)?((num)*100)/(max):0)
8 #define prof prof5i
9 
10 extern	Inst	itab[];
11 Inst *tables[] = { itab, 0 };
12 
13 void
isum(void)14 isum(void)
15 {
16 	Inst *i;
17 	int total, mems, arith, branch;
18 	int useddelay, taken, syscall;
19 	int pct, j;
20 
21 	total = 0;
22 	mems = 0;
23 	arith = 0;
24 	branch = 0;
25 	useddelay = 0;
26 	taken = 0;
27 	syscall = 0;
28 
29 	/* Compute the total so we can have percentages */
30 	for(i = itab; i->func; i++)
31 		if(i->name && i->count)
32 			total += i->count;
33 
34 	Bprint(bioout, "\nInstruction summary.\n\n");
35 
36 	for(j = 0; tables[j]; j++) {
37 		for(i = tables[j]; i->func; i++) {
38 			if(i->name) {
39 				/* This is gross */
40 				if(i->count == 0)
41 					continue;
42 				pct = Percent(i->count, total);
43 				if(pct != 0)
44 					Bprint(bioout, "%-8ud %3d%% %s\n",
45 						i->count, Percent(i->count,
46 						total), i->name);
47 				else
48 					Bprint(bioout, "%-8ud      %s\n",
49 						i->count, i->name);
50 
51 
52 				switch(i->type) {
53 				default:
54 					fatal(0, "isum bad stype %d\n", i->type);
55 				case Imem:
56 					mems += i->count;
57 					break;
58 				case Iarith:
59 					arith += i->count;
60 					break;
61 				case Ibranch:
62 					branch += i->count;
63 					taken += i->taken;
64 					useddelay += i->useddelay;
65 					break;
66 				case Isyscall:
67 					syscall += i->count;
68 					break;
69 				}
70 
71 			}
72 		}
73 	}
74 
75 	Bprint(bioout, "\n%-8ud      Memory cycles\n", mems+total);
76 	Bprint(bioout, "%-8ud %3d%% Instruction cycles\n",
77 			total, Percent(total, mems+total));
78 	Bprint(bioout, "%-8ud %3d%% Data cycles\n\n",
79 			mems, Percent(mems, mems+total));
80 
81 	Bprint(bioout, "%-8ud %3d%% Arithmetic\n",
82 			arith, Percent(arith, total));
83 
84 	Bprint(bioout, "%-8ud %3d%% System calls\n",
85 			syscall, Percent(syscall, total));
86 
87 	Bprint(bioout, "%-8ud %3d%% Branches\n",
88 			branch, Percent(branch, total));
89 
90 	Bprint(bioout, "   %-8ud %3d%% Branches taken\n",
91 			taken, Percent(taken, branch));
92 
93 	Bprint(bioout, "   %-8ud %3d%% Delay slots\n",
94 			useddelay, Percent(useddelay, branch));
95 
96 	Bprint(bioout, "   %-8ud %3d%% Unused delay slots\n",
97 			branch-useddelay, Percent(branch-useddelay, branch));
98 
99 	Bprint(bioout, "%-8ud %3d%% Program total delay slots\n",
100 			nopcount, Percent(nopcount, total));
101 }
102 
103 void
tlbsum(void)104 tlbsum(void)
105 {
106 	if(tlb.on == 0)
107 		return;
108 
109 	Bprint(bioout, "\n\nTlb summary\n");
110 
111 	Bprint(bioout, "\n%-8d User entries\n", tlb.tlbsize);
112 	Bprint(bioout, "%-8d Accesses\n", tlb.hit+tlb.miss);
113 	Bprint(bioout, "%-8d Tlb hits\n", tlb.hit);
114 	Bprint(bioout, "%-8d Tlb misses\n", tlb.miss);
115 	Bprint(bioout, "%7d%% Hit rate\n", Percent(tlb.hit, tlb.hit+tlb.miss));
116 }
117 
118 char *stype[] = { "Stack", "Text", "Data", "Bss" };
119 
120 void
segsum(void)121 segsum(void)
122 {
123 	Segment *s;
124 	int i;
125 
126 	Bprint(bioout, "\n\nMemory Summary\n\n");
127 	Bprint(bioout, "      Base     End      Resident References\n");
128 	for(i = 0; i < Nseg; i++) {
129 		s = &memory.seg[i];
130 		Bprint(bioout, "%-5s %.8lux %.8lux %-8d %-8d\n",
131 				stype[i], s->base, s->end, s->rss*BY2PG, s->refs);
132 	}
133 }
134 
135 typedef struct Prof Prof;
136 struct Prof
137 {
138 	Symbol	s;
139 	long	count;
140 };
141 Prof	prof[5000];
142 
143 int
profcmp(void * va,void * vb)144 profcmp(void *va, void *vb)
145 {
146 	Prof *a, *b;
147 
148 	a = va;
149 	b = vb;
150 	return b->count - a->count;
151 }
152 
153 void
iprofile(void)154 iprofile(void)
155 {
156 	Prof *p, *n;
157 	int i, b, e;
158 	ulong total;
159 	extern ulong textbase;
160 
161 	i = 0;
162 	p = prof;
163 	if(textsym(&p->s, i) == 0)
164 		return;
165 	i++;
166 	for(;;) {
167 		n = p+1;
168 		if(textsym(&n->s, i) == 0)
169 			break;
170 		b = (p->s.value-textbase)/PROFGRAN;
171 		e = (n->s.value-textbase)/PROFGRAN;
172 		while(b < e)
173 			p->count += iprof[b++];
174 		i++;
175 		p = n;
176 	}
177 
178 	qsort(prof, i, sizeof(Prof), profcmp);
179 
180 	total = 0;
181 	for(b = 0; b < i; b++)
182 		total += prof[b].count;
183 
184 	Bprint(bioout, "  cycles     %% symbol          file\n");
185 	for(b = 0; b < i; b++) {
186 		if(prof[b].count == 0)
187 			continue;
188 
189 		Bprint(bioout, "%8ld %3ld.%ld %-15s ",
190 			prof[b].count,
191 			100*prof[b].count/total,
192 			(1000*prof[b].count/total)%10,
193 			prof[b].s.name);
194 
195 		printsource(prof[b].s.value);
196 		Bputc(bioout, '\n');
197 	}
198 	memset(prof, 0, sizeof(Prof)*i);
199 }
200