2 * $Header: /cvs/sw/new-wave/user/tpt/build.c,v 1.1 2002-02-14 23:04:55 pauli Exp $
3 * "foo $Revision: 1.1 $ bar"
22 dprintf(char *fmt, ...)
29 ret = vprintf(fmt, ap);
38 tpt_node *ret = (tpt_node *)malloc(sizeof(*ret));
41 for (i = 0; i < TIMEPEG_NR_SUCCS; i++)
43 ret->succs[i].avg_tp = 0;
44 ret->succs[i].nr_times = 0;
45 ret->succs[i].min_tp = ~0;
46 ret->succs[i].max_tp = 0;
47 ret->succs[i].succ = 0;
58 while (*p && isspace(*p))
64 line(FILE *f, char *buf, int eofok)
68 if (fgets(buf, BUFSIZE, f) == 0)
72 fprintf(stderr, "unexpected EOF\n");
76 /* Chop trailing newline */
77 p = buf + strlen(buf);
79 if (p >= buf && *p == '\n')
86 want(int v1, int v2, const char * const _line)
90 fprintf(stderr, "error parsing line '%s'\n", _line);
91 fprintf(stderr, "expected %d items, got %d\n", v1, v2);
96 typedef struct linebuf
103 build_linebuf(FILE *f)
106 linebuf *lb = 0, *newlb;
111 if (line(f, buf, 1) == 0)
113 newlb = (linebuf *)malloc(sizeof(*lb));
114 newlb->line = strdup(buf);
130 find_node(tpt_node *list, const char * const name)
132 tpt_node *node = list;
136 if (strcmp(node->name, name) == 0)
144 must_find_node(tpt_node *list, const char * const name)
146 tpt_node *ret = find_node(list, name);
149 fprintf(stderr, "Internal error; Couldn't locate node `%s'\n", name);
160 "input parsing failed at line %d\n", __LINE__); \
165 dprintf("NEXTLB:%s\n", lb->line); \
169 build_graph(const char * const file_name, unsigned long mhz)
171 FILE *f = fopen(file_name, "r");
173 struct tpt_node *list = 0;
174 linebuf *linebufs, *lb;
179 fprintf(stderr, "can't open '%s'\n", file_name);
184 want(sscanf(buf, "%d", &nr_cpus), 1, buf);
187 linebufs = build_linebuf(f);
191 for (lb = linebufs; lb; lb = lb->next)
193 printf("%s\n", lb->line);
198 /* Pass 1: allocate data structures and name them */
206 new_node = make_new_node();
208 if (sscanf(lb->line, " \"%[^\"]\"", namebuf) != 1)
210 fprintf(stderr, "tpt: name parsing failed(1) on `%s'\n", lb->line);
213 new_node->name = strdup(namebuf);
215 for (i = 0; i < nr_cpus; i++)
221 if (sscanf(lb->line, " cpu%d", &cpu) != 1)
223 fprintf(stderr, "read of CPU field failed on '%s'\n", lb->line);
227 NEXTLB(); /* CPU identifier */
228 if (sscanf(lb->line, " %d", &nr_preds) != 1)
230 fprintf(stderr, "read of nr_preds failed on '%s'\n", lb->line);
234 for (j = 0; j < nr_preds; j++)
239 new_node->next = list;
243 /* Pass 2: fill in data */
246 * We do this by visiting each node and filling in its
247 * predecessor's successor field...
257 if (sscanf(lb->line, " \"%[^\"]\"", namebuf) != 1)
259 fprintf(stderr, "tpt: name parsing failed(2) on `%s'\n", lb->line);
262 node = must_find_node(list, namebuf);
263 NEXTLB(); /* Advance to CPU */
264 for (i = 0; i < nr_cpus; i++)
270 if (sscanf(lb->line, " cpu%d", &cpu) != 1)
272 fprintf(stderr, "read of CPU field failed on '%s'\n", lb->line);
275 NEXTLB(); /* CPU identifier */
277 sscanf(lb->line, " %d", &nr_preds);
280 for (j = 0; j < nr_preds; j++)
283 unsigned long avglo, avghi, minlo, minhi, maxlo, maxhi;
287 struct timepeg_arc *arc;
290 nf = sscanf( lb->line,
291 " \"%[^\"]\" %u %u:%u %u:%u %u:%u",
294 (int *)&avghi, (int *)&avglo,
295 (int *)&minhi, (int *)&minlo,
296 (int *)&maxhi, (int *)&maxlo);
299 fprintf(stderr, "error parsing `%s'\n", lb->line);
304 pred = find_node(list, predname);
307 fprintf(stderr, "tpt: I'm confused\n");
311 /* Does this successor already exist? */
313 for (succ_idx = 0; succ_idx < TIMEPEG_NR_SUCCS; succ_idx++)
315 if (pred->succs[succ_idx].succ == 0)
317 arc = &pred->succs[succ_idx];
319 assert(pred->nr_succs == succ_idx);
324 else if (pred->succs[succ_idx].succ == node)
325 { /* Another ref to this node */
326 arc = &pred->succs[succ_idx];
327 assert(arc->succ == node);
334 timepeg_t avg, min, max;
337 arc->nr_times += nr_times;
347 if (min < arc->min_tp)
353 if (max > arc->max_tp)
358 fprintf(stderr, "Internal error: TIMEPEG_NR_SUCCS is too small\n");