togcat.c
4.18 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_INPUT 256
#define CHUNK_TOGGLE 50000
#define CHUNK_STRING 100000
#define MAX_TOGGLE_CHUNKS 100
#define HASH_TABLE_SIZE 32768
struct toggle {
char *netname;
long flags;
long tog_to_0;
long tog_to_1;
long tog_sum;
};
struct hash_entry {
struct toggle *tp;
struct hash_entry *next;
};
struct hash_entry hash_table[HASH_TABLE_SIZE];
struct toggle *toggle_tbl[MAX_TOGGLE_CHUNKS];
int tableindex, subindex, stringbytes;
struct toggle *freetp;
char *stringarea;
char line[MAX_INPUT];
void printtogtbl(void);
struct toggle *locate(unsigned char *);
struct toggle *newnet(char *);
void addfile(FILE *);
void inittogtbl(void);
main(int argc, char *argv[])
{
int i;
FILE *infile;
if (argc < 3) {
fprintf(stderr, "Usage: %s f1.tog ... fn.tog > output\n", argv[0]);
exit(1);
}
i = 1;
inittogtbl();
while ( i < argc) {
if ((infile = fopen(argv[i], "r")) == NULL) {
fprintf(stderr, "cannot open %s\n", argv[i]);
}
addfile(infile);
i++;
}
printtogtbl();
}
void
inittogtbl()
{
/* jump start the table & string area */
toggle_tbl[0] = (struct toggle *)calloc(CHUNK_TOGGLE, sizeof(struct toggle));
freetp = toggle_tbl[0];
tableindex = 0;
subindex = 0;
stringarea = (char *)malloc(CHUNK_STRING);
stringbytes = CHUNK_STRING;
}
void
addfile(FILE *in)
{
long sum, t0, t1;
char netname[256];
struct toggle *tp;
while (fgets(line, MAX_INPUT, in) != NULL) {
if (sscanf(line, "%d %d %d %s\n", &sum, &t0, &t1, netname) == 4) {
tp = locate(netname);
tp->tog_to_0 += t0;
tp->tog_to_1 += t1;
tp->tog_sum = tp->tog_to_0 + tp->tog_to_1;
}
}
}
struct toggle *
newnet(char *netname)
{
int namesize;
struct toggle *tp;
tp = freetp++;
/* add to toggle list */
namesize = strlen(netname) + 1;
/* check for string area overflow */
if (namesize > stringbytes) {
stringarea = (char *)malloc(CHUNK_STRING);
stringbytes = CHUNK_STRING;
}
strcpy(stringarea, netname);
tp->netname = stringarea;
stringarea += namesize;
stringbytes -= namesize;
tp->tog_to_0 = 0;
tp->tog_to_1 = 0;
tp->tog_sum = 0;
tp->flags = 0;
subindex++;
/* check for chunk overflow */
if (subindex == CHUNK_TOGGLE) {
tableindex++;
if (tableindex == MAX_TOGGLE_CHUNKS) {
fprintf(stderr, "oh S---, huge number of nodes\n");
exit(1);
}
toggle_tbl[tableindex] = (struct toggle *)calloc(CHUNK_TOGGLE,
sizeof(struct toggle));
freetp = toggle_tbl[tableindex];
subindex = 0;
}
return tp;
}
/*
* Find the entry in the table. If it does not exist, add it
*/
struct toggle *
locate(unsigned char *name)
{
unsigned long hashvalue;
unsigned char c, *s;
struct hash_entry *hp;
/* first generate the hash number */
hashvalue = 0;
s = name;
while (1) {
c = *s++;
if (!c)
break;
hashvalue <<= 1;
hashvalue += c;
}
hashvalue %= HASH_TABLE_SIZE;
hp = &hash_table[hashvalue];
if (hp->tp == 0) {
/* need to add */
hash_table[hashvalue].tp = newnet(name);
return hash_table[hashvalue].tp;
} else {
/* walk down the chain looking for a match */
while (hp != 0) {
if (strcmp(name, hp->tp->netname) == 0) {
return hp->tp;
} else {
hp = hp->next;
}
}
if (hp == NULL) {
/* did not find match in chain */
hp = (struct hash_entry *)malloc(sizeof(struct hash_entry));
hp->tp = hash_table[hashvalue].tp;
hp->next = hash_table[hashvalue].next;
hash_table[hashvalue].tp = newnet(name);
hash_table[hashvalue].next = hp;
return hash_table[hashvalue].tp;
}
}
}
void
printtogtbl()
{
int i;
int tableindex;
struct toggle *tp;
tableindex = 0;
i = 0;
tp = toggle_tbl[tableindex];
printf("-- TOGGLE COUNTS (format: sum toggle_to_0 toggle_to_1 net_name)\n");
while (tp) {
if (tp->netname ) {
printf("%6d %6d %6d %s\n", tp->tog_sum, tp->tog_to_0,
tp->tog_to_1, tp->netname);
}
i++;
if (i == CHUNK_TOGGLE) {
tableindex++;
tp = toggle_tbl[tableindex];
i = 0;
} else {
tp++;
}
}
}