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