-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtally.c
161 lines (143 loc) · 3.36 KB
/
tally.c
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
#include <string.h>
int selected = 0;
void sel() {
selected = 1;
}
void desel() {
selected = 0;
}
int issel() {
return selected;
}
char *tag[1000];
int found=0;
char *intern (char *string) {
for (int i=0; i<found; i++) {
if (!strcmp(string, tag[i])) {
return tag[i];
}
}
if (found >= 1000) {
YY_DIE("tally: intern: too many unique strings\n");
}
tag[found] = strdup(string);
return tag[found++];
}
struct tally {
char *parent, *child;
int count, samples, period;
long long int offsets[10];
int lengths[10];
} arc[1000];
int total=0, uniq=0;
void dot () {
FILE *fp = fopen("tally.dot", "w");
fprintf(fp, "digraph xml {\n");
fprintf(fp, "node [fillcolor=gold, style=filled, fontname=\"Monaco\"];\n");
for (int i=0; i<uniq; i++) {
fprintf(fp, "\"%s\" -> \"%s\" [ label = \"%d\", fontname=\"Monaco\", URL=\"javascript:top.samples([", arc[i].parent, arc[i].child, arc[i].count);
for (int j=0; j<arc[i].samples; j++) {
fprintf(fp, "'%llu-%d'", arc[i].offsets[j], arc[i].lengths[j]);
if (j < (arc[i].samples - 1)) {
fprintf(fp, ",");
}
}
fprintf(fp, "]);\"];\n");
}
fprintf(fp, "}\n");
fclose(fp);
}
void degrade (struct tally *s) {
for (int i=0, j=0; i<s->samples; i++, i++, j++) {
s->offsets[j] = s->offsets[i];
s->lengths[j] = s->lengths[i];
}
s->samples = s->samples / 2;
s->period = s->period * 2;
}
void aggrade (struct tally *s) {
if (s->samples >= 10) {
degrade (s);
} else {
s->offsets[s->samples] = yyaccepted+yythisthunk->mybegin;
s->lengths[s->samples] = yythisthunk->myend > yythisthunk->mybegin ? yythisthunk->myend - yythisthunk->mybegin : 0;
s->samples++;
sel();
}
}
void regrade (struct tally *s) {
// find sample (how? assume last, which won't survive degrade)
int i = s->samples-1;
// adjust length based on new yythisthunk
int begin = ((long long)s->offsets[i])-yyaccepted;
if (yythisthunk->myend > begin+4000) return;
s->lengths[i] = yythisthunk->myend > begin ? yythisthunk->myend - begin : 0;
}
int add (char *p, char *c) {
for (int i=0; i<uniq; i++) {
if (arc[i].parent == p && arc[i].child == c) {
arc[i].count++;
if (!(arc[i].count % arc[i].period)) {
aggrade (&arc[i]);
return i;
}
return -1;
}
}
if (uniq >= 1000) {
YY_DIE("tally: add: too many arcs\n");
}
arc[uniq].parent = p;
arc[uniq].child = c;
arc[uniq].count = 1;
arc[uniq].samples = 0;
arc[uniq].period = 1;
aggrade (&arc[uniq]);
uniq++;
return uniq-1;
}
char *nest[1000] = {"/root/"};
//int sediment[1000];
int depth = 1;
int limit = 3000;
int trace(int omit) {
if (limit > 0) {
limit--;
for (int i = omit; i<depth; i++) {
fprintf(stderr, " ");
}
return 1;
}
return 0;
}
void push (char *string) {
// if (trace(0)) fprintf(stderr, "push %s\n", string);
char *tag = intern(string);
//sediment[depth] = add(nest[depth-1], tag);
if (depth >= 1000) {
YY_DIE("tally: push: too deeply nested\n");
}
nest[depth++] = tag;
}
void pop (char *string) {
// if (trace(1)) fprintf(stderr, "pop %s\n", string);
if (depth == 1) {
return;
}
add(nest[depth-2], nest[depth-1]);
if (strcmp(nest[--depth], string)) {
pop(string);
} else {
//int i = sediment[depth];
//if (i >= 0) {
// regrade (&arc[i]);
//}
}
}
void pip (char *string) {
push(string);
pop(string);
}
void pup () {
--depth;
}