aboutsummaryrefslogtreecommitdiff
path: root/src/timer.c
blob: ae026d99851784dc9fc898dadeab00ff79e9a149 (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* wydawca - automatic release submission daemon
   Copyright (C) 2008-2013, 2017, 2019-2020 Sergey Poznyakoff

   Wydawca is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   Wydawca is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with wydawca. If not, see <http://www.gnu.org/licenses/>. */

#include <wydawca.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>

typedef struct timer_slot {
    size_t refcnt;
    double real;
    double self_user;		/* user time in sec */
    double self_system;		/* system time in sec */
    double children_user;	/* user time in sec */
    double children_system;	/* system time in sec */
    struct timeval real_mark;
    struct rusage self_mark;
    struct rusage children_mark;
} WY_TIMER;

static pthread_key_t key;
static pthread_once_t key_once = PTHREAD_ONCE_INIT;

static void
timer_free(void *f)
{
    free(f);
}

static void
make_key(void)
{
    pthread_key_create(&key, timer_free);
}

#define MAX_TIMERS (MY_TIMER_SPOOL_FIRST + spool_count)

WY_TIMER *
get_thread_timer_table(void)
{
    WY_TIMER *timer_table;
    
    pthread_once(&key_once, make_key);
    if ((timer_table = pthread_getspecific(key)) == NULL) {
	timer_table = grecs_calloc(MAX_TIMERS,
				   sizeof(timer_table[0]));
	pthread_setspecific(key, timer_table);
    }
    return timer_table;
}

wydawca_timer_t
get_thread_timer(int n)
{
    if (n < 0 || n >= MAX_TIMERS)
	abort();
    return get_thread_timer_table() + n;
}

wydawca_timer_t
timer_start(int idx)
{
    wydawca_timer_t t = get_thread_timer(idx);

    if (t->refcnt++ == 0) {
	gettimeofday(&t->real_mark, NULL);
	getrusage(RUSAGE_SELF, &t->self_mark);
	getrusage(RUSAGE_CHILDREN, &t->children_mark);
    }
    return t;
}

#define DIFFTIME(now,then)\
   (((now).tv_sec - (then).tv_sec) \
    + ((double)((now).tv_usec - (then).tv_usec))/1000000)

static void
_timer_compute(wydawca_timer_t t)
{
    struct timeval real;
    struct rusage rusage;

    gettimeofday(&real, NULL);
    t->real = DIFFTIME(real, t->real_mark);
    getrusage(RUSAGE_SELF, &rusage);
    t->self_user = DIFFTIME(rusage.ru_utime, t->self_mark.ru_utime);
    t->self_system = DIFFTIME(rusage.ru_stime, t->self_mark.ru_stime);

    getrusage(RUSAGE_CHILDREN, &rusage);
    t->children_user =
	DIFFTIME(rusage.ru_utime, t->children_mark.ru_utime);
    t->children_system =
	DIFFTIME(rusage.ru_stime, t->children_mark.ru_stime);
}

static inline void
_timer_reset(wydawca_timer_t t)
{
    t->real = 0.0;
    t->self_user = 0.0;
    t->self_system = 0.0;
    t->children_user = 0.0;
    t->children_system = 0.0;
}

wydawca_timer_t
timer_get(int idx)
{
    wydawca_timer_t t = get_thread_timer(idx);
    if (t->refcnt) 
	_timer_compute(t);
    return t;
}

wydawca_timer_t
timer_stop(int idx)
{
    wydawca_timer_t t = timer_get(idx);
    if (t->refcnt)
	--t->refcnt;
    return t;
}

wydawca_timer_t
timer_reset(int idx)
{
    wydawca_timer_t t = get_thread_timer(idx);
    _timer_reset(t);
    return t;
}

double
timer_get_real(wydawca_timer_t t)
{
    return t->real;
}

double
timer_get_user(wydawca_timer_t t)
{
    return t->self_user + t->children_user;
}

double
timer_get_system(wydawca_timer_t t)
{
    return t->self_system + t->children_system;
}

char *
timer_format_time(double t)
{
    char *str = NULL;
    size_t size = 0;
    if (t < 600)
	grecs_asprintf(&str, &size, "%0.3f", t);
    else {
	long int s, m, h, d;

	s = (long int) t;
	d = s / (3600 * 24);
	s -= d * 3600 * 24;
	h = s / 3600;
	s -= h * 3600;
	m = s / 60;
	s -= m * 60;

	if (d)
	    grecs_asprintf(&str, &size, "%ld+%02ld:%02ld:%02ld", d,
			   h, m, s);
	else if (h)
	    grecs_asprintf(&str, &size, "%02ld:%02ld:%02ld", h, m, s);
	else
	    grecs_asprintf(&str, &size, "%02ld:%02ld", m, s);
    }
    if (!str)
	grecs_alloc_die();
    return str;
}

/* Cumulative statistic counters and timers */
WY_STAT_COUNTER wydawca_global_stats[WY_MAX_STAT];
WY_TIMER *wydawca_global_timer_table;
pthread_mutex_t global_stats_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t global_stats_cond = PTHREAD_COND_INITIALIZER;
static int stat_stop;
pthread_t stat_tid;

void
wydawca_stat_add(int what, size_t n)
{
    if (what >= WY_MAX_STAT) abort();
    pthread_mutex_lock(&global_stats_mutex);
    wydawca_global_stats[what] += n;
    pthread_mutex_unlock(&global_stats_mutex);
}

/* When running in cron mode, this function is called by the triplet
   processing thread when it has finished processing.  The function
   incorporates the thread-specific statistics into the cumulative
   counters. */
void
wydawca_stat_update(void)
{
    int i;
    WY_TIMER *thread_timers = get_thread_timer_table();
    pthread_mutex_lock(&global_stats_mutex);
//    _timer_compute(&wydawca_global_timer_table[WY_TIMER_WYDAWCA]);
    for (i = 0; i < MAX_TIMERS; i++) {
	wydawca_global_timer_table[i].real
	    += thread_timers[i].real;
	wydawca_global_timer_table[i].self_user
	    += thread_timers[i].self_user;
	wydawca_global_timer_table[i].self_system
	    += thread_timers[i].self_system;
	wydawca_global_timer_table[i].children_user
	    += thread_timers[i].children_user;
	wydawca_global_timer_table[i].children_system
	    += thread_timers[i].children_system;
    }
    pthread_mutex_unlock(&global_stats_mutex);
}

void
wydawca_stat_notify(int final)
{
    void *ret;
    pthread_mutex_lock(&global_stats_mutex);
    stat_stop = final;
    pthread_cond_broadcast(&global_stats_cond);
    pthread_mutex_unlock(&global_stats_mutex);
    if (final)
	pthread_join(stat_tid, &ret);
}

void
wydawca_stat_reset(void)
{
    int i;
    
    for (i = 0; i < WY_MAX_STAT; i++)
	wydawca_global_stats[i] = 0;
    for (i = 0; i < MAX_TIMERS; i++)
	_timer_reset(&wydawca_global_timer_table[i]);
}

time_t stat_report_interval = DEFAULT_STAT_REPORT_INTERVAL;

void *
wy_thr_stat(void *ptr)
{
    wy_set_cur_thread_name("statcol");
    pthread_mutex_lock(&global_stats_mutex);
    /* export the thread's specific stats and timer pointers to
       wydawca_global_stats and wydawca_global_stats. */
    wydawca_global_timer_table = get_thread_timer_table();
    while (!stat_stop) {
	struct timespec ts;
	clock_gettime(CLOCK_REALTIME, &ts);
	ts.tv_sec += stat_report_interval;
	pthread_cond_timedwait(&global_stats_cond, &global_stats_mutex, &ts);
	wydawca_stat_log();
	notify_finish();
	wydawca_stat_reset();
    }
    pthread_mutex_unlock(&global_stats_mutex);
    return NULL;
}

void
wydawca_stat_init(void)
{
    pthread_create(&stat_tid, NULL, wy_thr_stat, NULL);
}

static char *stat_name[WY_MAX_STAT] = {
    N_("errors"),
    N_("warnings"),
    N_("bad signatures"),
    N_("access violation attempts"),
    N_("complete triplets"),
    N_("incomplete triplets"),
    N_("bad triplets"),
    N_("expired triplets"),
    N_("triplet successes"),
    N_("files uploaded"),
    N_("files archived"),
    N_("symlinks created"),
    N_("symlinks removed"),
    N_("check failures"),
};

static char *stat_kwname[WY_MAX_STAT] = {
    "stat:errors",
    "stat:warnings",
    "stat:bad_signatures",
    "stat:access_violations",
    "stat:complete_triplets",
    "stat:incomplete_triplets",
    "stat:bad_triplets",
    "stat:expired_triplets",
    "stat:triplet_success",
    "stat:uploads",
    "stat:archives",
    "stat:symlinks",
    "stat:rmsymlinks",
    "stat:check_failures"
};

int
wy_stat_mask_p(unsigned long mask)
{
    int i;

    for (i = 0; i < WY_MAX_STAT; i++)
	if (wydawca_global_stats[i] != 0 && (mask && WY_STAT_MASK(i)))
	    return 1;
    return 0;
}

int
wy_stat_expansion(char **ret, char const *name, size_t len)
{
    int i;

    for (i = 0; i < WY_MAX_STAT; i++) {
	if (strlen(stat_kwname[i]) == len
	    && memcmp(stat_kwname[i], name, len) == 0) {
	    size_t size = 0;
	    *ret = NULL;
	    if (grecs_asprintf(ret, &size, "%u", wydawca_global_stats[i]))
		return WRDSE_NOSPACE;
	    else
		return WRDSE_OK;
	}
    }
    return WRDSE_UNDEF;
}

void
wydawca_stat_log(void)
{
    int i;

    if (wy_stat_mask_p(print_stats)) {
	for (i = 0; i < WY_MAX_STAT; i++)
	    if (print_stats & WY_STAT_MASK(i))
		wy_log(LOG_INFO, "%s: %u",
		       gettext(stat_name[i]), wydawca_global_stats[i]);
    }
}


Return to:

Send suggestions and report system problems to the System administrator.