ack/util/int/tally.c

147 lines
2.7 KiB
C
Raw Normal View History

1988-06-22 16:57:09 +00:00
/*
2019-03-17 14:42:00 +00:00
Gathering run-time statistics
*/
1988-06-22 16:57:09 +00:00
1994-06-24 11:31:16 +00:00
/* $Id$ */
1988-06-22 16:57:09 +00:00
#include <stdio.h>
#include "global.h"
#include "linfil.h"
#include "alloc.h"
#include "whatever.h"
1988-06-22 16:57:09 +00:00
2019-03-17 14:42:00 +00:00
struct line_tally
{ /* one for each line */
long lt_cnt; /* counts entrances */
long lt_instr; /* counts instructions */
1988-06-22 16:57:09 +00:00
};
2019-03-17 14:42:00 +00:00
struct file_tally
{ /* one for each file */
1988-06-22 16:57:09 +00:00
struct file_tally *next;
2019-03-17 14:42:00 +00:00
ptr ft_fil; /* file name */
long ft_limit; /* size of line array */
struct line_tally *ft_line; /* pointer to line array */
1988-06-22 16:57:09 +00:00
};
2019-03-17 14:42:00 +00:00
PRIVATE struct file_tally *first_tally; /* start of chain */
PRIVATE struct file_tally *file; /* present file */
1988-06-22 16:57:09 +00:00
PRIVATE long lastLIN;
2019-03-17 14:42:00 +00:00
PRIVATE FILE *tally_fp;
/* Forward declarations. */
PRIVATE void tally_newFIL(ptr);
PRIVATE void enlarge(struct file_tally *, long);
1988-06-22 16:57:09 +00:00
2019-03-17 14:42:00 +00:00
void tally(void)
1988-06-22 16:57:09 +00:00
{
if (!FIL)
return;
2019-03-17 14:42:00 +00:00
if (!file || FIL != file->ft_fil)
{
1988-06-22 16:57:09 +00:00
tally_newFIL(FIL);
file->ft_fil = FIL;
lastLIN = -1;
}
2019-03-17 14:42:00 +00:00
if (LIN != lastLIN)
{
if (LIN >= file->ft_limit)
{
1988-06-22 16:57:09 +00:00
enlarge(file, LIN);
}
file->ft_line[LIN].lt_cnt++;
lastLIN = LIN;
}
file->ft_line[LIN].lt_instr++;
}
2019-03-17 14:42:00 +00:00
PRIVATE void tally_newFIL(ptr f)
1988-06-22 16:57:09 +00:00
{
struct file_tally **hook = &first_tally;
2019-03-17 14:42:00 +00:00
while (*hook)
{
1988-06-22 16:57:09 +00:00
if ((*hook)->ft_fil == f)
break;
hook = &(*hook)->next;
}
2019-03-17 14:42:00 +00:00
if (!*hook)
{
1988-06-22 16:57:09 +00:00
/* first time we see this file */
/* construct a new entry */
2019-03-17 14:42:00 +00:00
struct file_tally *nt = (struct file_tally *) Malloc(
(size) sizeof(struct file_tally), "file_tally");
nt->next = (struct file_tally *) 0;
1988-06-22 16:57:09 +00:00
nt->ft_fil = f;
2019-03-17 14:42:00 +00:00
nt->ft_limit = 1; /* provisional length */
nt->ft_line = (struct line_tally *) Malloc(
(size) sizeof(struct line_tally), "struct line_tally");
1988-06-22 16:57:09 +00:00
nt->ft_line[0].lt_cnt = 0;
nt->ft_line[0].lt_instr = 0;
2019-03-17 14:42:00 +00:00
1988-06-22 16:57:09 +00:00
/* and hook it in */
*hook = nt;
}
file = *hook;
}
2019-03-17 14:42:00 +00:00
PRIVATE void enlarge(struct file_tally *ft, long l)
1988-06-22 16:57:09 +00:00
{
long limit = allocfrac(l < 100 ? 100 : l);
2019-03-17 14:42:00 +00:00
1988-06-22 16:57:09 +00:00
if (limit <= ft->ft_limit)
return;
2019-03-17 14:42:00 +00:00
ft->ft_line = (struct line_tally *) Realloc((char *) ft->ft_line,
(size) (limit * sizeof(struct line_tally)), "array line_tally");
while (ft->ft_limit < limit)
{
1988-06-22 16:57:09 +00:00
ft->ft_line[ft->ft_limit].lt_cnt = 0;
ft->ft_line[ft->ft_limit].lt_instr = 0;
ft->ft_limit++;
}
}
2019-03-17 14:42:00 +00:00
void out_tally(void)
1988-06-22 16:57:09 +00:00
{
struct file_tally **hook = &first_tally;
2019-03-17 14:42:00 +00:00
1988-06-22 16:57:09 +00:00
if (!*hook)
return;
2022-07-18 17:36:44 +00:00
tally_fp = fopen("int.tally", "wb");
1988-06-22 16:57:09 +00:00
if (!tally_fp)
return;
2019-03-17 14:42:00 +00:00
while (*hook)
{
1988-06-22 16:57:09 +00:00
struct file_tally *ft = *hook;
register long i;
2019-03-17 14:42:00 +00:00
1988-06-22 16:57:09 +00:00
fprintf(tally_fp, "%s:\n", dt_fname(ft->ft_fil));
2019-03-17 14:42:00 +00:00
for (i = 0; i < ft->ft_limit; i++)
{
1988-06-22 16:57:09 +00:00
struct line_tally *lt = &ft->ft_line[i];
2019-03-17 14:42:00 +00:00
if (lt->lt_cnt)
{
1988-06-22 16:57:09 +00:00
/* we visited this line */
2019-03-17 14:42:00 +00:00
fprintf(tally_fp, "\t%ld\t%ld\t%ld\n", i, lt->lt_cnt,
lt->lt_instr);
1988-06-22 16:57:09 +00:00
}
}
fprintf(tally_fp, "\n");
hook = &(*hook)->next;
}
fclose(tally_fp);
tally_fp = 0;
}