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
|
/*
Task Spooler - a task queue system for the unix user
Copyright (C) 2007-2009 LluĂs Batlle i Rossell
Please find the license in the provided COPYING file.
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/time.h>
#include "main.h"
void pinfo_init(struct Procinfo *p)
{
p->ptr = 0;
p->nchars = 0;
p->allocchars = 0;
p->start_time.tv_sec = 0;
p->start_time.tv_usec = 0;
p->end_time.tv_sec = 0;
p->end_time.tv_usec = 0;
p->enqueue_time.tv_sec = 0;
p->enqueue_time.tv_usec = 0;
}
void pinfo_free(struct Procinfo *p)
{
if (p->ptr)
{
free(p->ptr);
}
p->nchars = 0;
p->allocchars = 0;
}
void pinfo_addinfo(struct Procinfo *p, int maxsize, const char *line, ...)
{
va_list ap;
int newchars = p->nchars + maxsize;
void *newptr;
int res;
va_start(ap, line);
/* Ask for more memory for the array, if needed */
if (newchars > p->allocchars)
{
int newmem;
int newalloc;
newalloc = newchars;
newmem = newchars * sizeof(*p->ptr);
newptr = realloc(p->ptr, newmem);
if(newptr == 0)
{
warning("Cannot realloc more memory (%i) in pinfo_addline. "
"Not adding the content.", newmem);
return;
}
p->ptr = (char *) newptr;
p->allocchars = newalloc;
}
res = vsnprintf(p->ptr + p->nchars, (p->allocchars - p->nchars), line, ap);
p->nchars += res; /* We don't store the final 0 */
}
void pinfo_dump(const struct Procinfo *p, int fd)
{
if (p->ptr)
{
int res;
int rest = p->nchars;
while (rest > 0)
{
res = write(fd, p->ptr, rest);
if (res == -1)
{
warning("Cannot write more chars in pinfo_dump");
return;
}
rest -= res;
}
}
}
int pinfo_size(const struct Procinfo *p)
{
return p->nchars;
}
void pinfo_set_enqueue_time(struct Procinfo *p)
{
gettimeofday(&p->enqueue_time, 0);
p->start_time.tv_sec = 0;
p->start_time.tv_usec = 0;
p->end_time.tv_sec = 0;
p->end_time.tv_usec = 0;
}
void pinfo_set_start_time(struct Procinfo *p)
{
gettimeofday(&p->start_time, 0);
p->end_time.tv_sec = 0;
p->end_time.tv_usec = 0;
}
void pinfo_set_end_time(struct Procinfo *p)
{
gettimeofday(&p->end_time, 0);
}
float pinfo_time_until_now(const struct Procinfo *p)
{
float t;
struct timeval now;
gettimeofday(&now, 0);
t = now.tv_sec - p->start_time.tv_sec;
t += (float) (now.tv_usec - p->start_time.tv_usec) / 1000000.;
return t;
}
float pinfo_time_run(const struct Procinfo *p)
{
float t;
t = p->end_time.tv_sec - p->start_time.tv_sec;
t += (float) (p->end_time.tv_usec - p->start_time.tv_usec) / 1000000.;
return t;
}
|