author | viric <viriketo@gmail.com> |
Thu, 01 Sep 2011 20:01:13 +0200 | |
changeset 299 | 73880166685f |
parent 267 | 11631dd11ff8 |
permissions | -rw-r--r-- |
165
7b914d4003c2
Fixing licenses in some files, and updating some documents.
viric@mandarina
parents:
147
diff
changeset
|
1 |
/* |
7b914d4003c2
Fixing licenses in some files, and updating some documents.
viric@mandarina
parents:
147
diff
changeset
|
2 |
Task Spooler - a task queue system for the unix user |
267 | 3 |
Copyright (C) 2007-2009 LluĂs Batlle i Rossell |
165
7b914d4003c2
Fixing licenses in some files, and updating some documents.
viric@mandarina
parents:
147
diff
changeset
|
4 |
|
7b914d4003c2
Fixing licenses in some files, and updating some documents.
viric@mandarina
parents:
147
diff
changeset
|
5 |
Please find the license in the provided COPYING file. |
7b914d4003c2
Fixing licenses in some files, and updating some documents.
viric@mandarina
parents:
147
diff
changeset
|
6 |
*/ |
146 | 7 |
#include <stdlib.h> |
8 |
#include <string.h> |
|
9 |
#include <unistd.h> |
|
10 |
#include <stdio.h> |
|
11 |
#include <stdarg.h> |
|
12 |
#include <sys/time.h> |
|
13 |
#include "main.h" |
|
14 |
||
147 | 15 |
void pinfo_init(struct Procinfo *p) |
16 |
{ |
|
17 |
p->ptr = 0; |
|
18 |
p->nchars = 0; |
|
19 |
p->allocchars = 0; |
|
20 |
p->start_time.tv_sec = 0; |
|
21 |
p->start_time.tv_usec = 0; |
|
22 |
p->end_time.tv_sec = 0; |
|
23 |
p->end_time.tv_usec = 0; |
|
24 |
p->enqueue_time.tv_sec = 0; |
|
25 |
p->enqueue_time.tv_usec = 0; |
|
26 |
} |
|
27 |
||
146 | 28 |
void pinfo_free(struct Procinfo *p) |
29 |
{ |
|
30 |
if (p->ptr) |
|
31 |
{ |
|
32 |
free(p->ptr); |
|
33 |
} |
|
34 |
p->nchars = 0; |
|
35 |
p->allocchars = 0; |
|
36 |
} |
|
37 |
||
147 | 38 |
void pinfo_addinfo(struct Procinfo *p, int maxsize, const char *line, ...) |
146 | 39 |
{ |
40 |
va_list ap; |
|
41 |
||
42 |
int newchars = p->nchars + maxsize; |
|
43 |
void *newptr; |
|
44 |
int res; |
|
45 |
||
46 |
va_start(ap, line); |
|
47 |
||
48 |
/* Ask for more memory for the array, if needed */ |
|
49 |
if (newchars > p->allocchars) |
|
50 |
{ |
|
51 |
int newmem; |
|
52 |
int newalloc; |
|
53 |
newalloc = newchars; |
|
54 |
newmem = newchars * sizeof(*p->ptr); |
|
55 |
newptr = realloc(p->ptr, newmem); |
|
56 |
if(newptr == 0) |
|
57 |
{ |
|
58 |
warning("Cannot realloc more memory (%i) in pinfo_addline. " |
|
59 |
"Not adding the content.", newmem); |
|
60 |
return; |
|
61 |
} |
|
62 |
p->ptr = (char *) newptr; |
|
63 |
p->allocchars = newalloc; |
|
64 |
} |
|
65 |
||
246
239b28c0cca9
Adding information on the exit status (signal/exit code).
viric@mandarina
parents:
231
diff
changeset
|
66 |
res = vsnprintf(p->ptr + p->nchars, (p->allocchars - p->nchars), line, ap); |
147 | 67 |
p->nchars += res; /* We don't store the final 0 */ |
146 | 68 |
} |
69 |
||
70 |
void pinfo_dump(const struct Procinfo *p, int fd) |
|
71 |
{ |
|
72 |
if (p->ptr) |
|
73 |
{ |
|
74 |
int res; |
|
75 |
int rest = p->nchars; |
|
76 |
while (rest > 0) |
|
77 |
{ |
|
78 |
res = write(fd, p->ptr, rest); |
|
79 |
if (res == -1) |
|
80 |
{ |
|
81 |
warning("Cannot write more chars in pinfo_dump"); |
|
82 |
return; |
|
83 |
} |
|
84 |
rest -= res; |
|
85 |
} |
|
86 |
} |
|
87 |
} |
|
88 |
||
89 |
int pinfo_size(const struct Procinfo *p) |
|
90 |
{ |
|
91 |
return p->nchars; |
|
92 |
} |
|
93 |
||
94 |
void pinfo_set_enqueue_time(struct Procinfo *p) |
|
95 |
{ |
|
96 |
gettimeofday(&p->enqueue_time, 0); |
|
97 |
p->start_time.tv_sec = 0; |
|
98 |
p->start_time.tv_usec = 0; |
|
99 |
p->end_time.tv_sec = 0; |
|
100 |
p->end_time.tv_usec = 0; |
|
101 |
} |
|
102 |
||
103 |
void pinfo_set_start_time(struct Procinfo *p) |
|
104 |
{ |
|
105 |
gettimeofday(&p->start_time, 0); |
|
106 |
p->end_time.tv_sec = 0; |
|
107 |
p->end_time.tv_usec = 0; |
|
108 |
} |
|
109 |
||
110 |
void pinfo_set_end_time(struct Procinfo *p) |
|
111 |
{ |
|
112 |
gettimeofday(&p->end_time, 0); |
|
113 |
} |
|
114 |
||
115 |
float pinfo_time_until_now(const struct Procinfo *p) |
|
116 |
{ |
|
117 |
float t; |
|
118 |
struct timeval now; |
|
119 |
||
120 |
gettimeofday(&now, 0); |
|
121 |
||
122 |
t = now.tv_sec - p->start_time.tv_sec; |
|
123 |
t += (float) (now.tv_usec - p->start_time.tv_usec) / 1000000.; |
|
124 |
||
125 |
return t; |
|
126 |
} |
|
127 |
||
128 |
float pinfo_time_run(const struct Procinfo *p) |
|
129 |
{ |
|
130 |
float t; |
|
131 |
||
132 |
t = p->end_time.tv_sec - p->start_time.tv_sec; |
|
133 |
t += (float) (p->end_time.tv_usec - p->start_time.tv_usec) / 1000000.; |
|
134 |
||
135 |
return t; |
|
136 |
} |