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