/
hbtest3.c
1395 lines (1103 loc) · 31.8 KB
/
hbtest3.c
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ------------------------------------------------------------------------- *
* Copyright (C) 2013-2014 Jolla Ltd.
* Contact: Simo Piiroinen <simo.piiroinen@jollamobile.com>
* License: LGPLv2.1
* ------------------------------------------------------------------------- */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdbool.h>
#include <errno.h>
#include <getopt.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <mce/dbus-names.h>
#include "../src/libiphb.h"
#include <sys/time.h>
/* How strictly we want to interpret the maximum wakup time */
#define ALLOWED_DELAY 999 // ms
/* Should we use monotonic time source or system time for timing */
#define USE_MONOTONIC_TIME 0
typedef struct hbtimer_t hbtimer_t;
/* -- tv -- */
static void tv_get_monotime(struct timeval *tv);
static int tv_diff_in_ms (const struct timeval *tv1, const struct timeval *tv2);
/* -- log -- */
static void log_emit(const char *fmt, ...);
/* -- mainloop -- */
static void mainloop_stop(int exit_code);
static int mainloop_run (void);
/* -- systembus -- */
static bool systembus_connect (void);
static void systembus_disconnect(void);
/* -- xmce -- */
static int xmce_cpu_keepalive_period(void);
static bool xmce_cpu_keepalive_start (void);
static bool xmce_cpu_keepalive_stop (void);
/* -- hbtimer -- */
static void hbtimer_show_stats (const hbtimer_t *self);
static gboolean hbtimer_handle_wakeup_renew_cb (gpointer aptr);
static gboolean hbtimer_handle_wakeup_finish_cb(gpointer aptr);
static void hbtimer_cancel_timers (hbtimer_t *self);
static void hbtimer_start_timers (hbtimer_t *self);
static void hbtimer_set_start (hbtimer_t *self, void (*cb) (hbtimer_t*));
static void hbtimer_set_finish (hbtimer_t *self, void (*cb) (hbtimer_t*), int secs);
static void hbtimer_set_renew (hbtimer_t *self, void (*cb) (hbtimer_t*), int secs);
static bool hbtimer_deactivate (hbtimer_t *self);
static void hbtimer_activate (hbtimer_t *self);
static void hbtimer_handle_wakeup_finish (hbtimer_t *self);
static void hbtimer_handle_wakeup_start (hbtimer_t *self);
static gboolean hbtimer_wakeup_cb (GIOChannel *chan, GIOCondition condition, gpointer data);
static void hbtimer_disconnect (hbtimer_t *self);
static bool hbtimer_connect (hbtimer_t *self);
static bool hbtimer_start (hbtimer_t *self);
static void hbtimer_ctor (hbtimer_t *self);
static void hbtimer_dtor (hbtimer_t *self);
static void hbtimer_setup (hbtimer_t *self, int mintime, int maxtime, int repeats);
static hbtimer_t *hbtimer_create (int mintime, int maxtime, int repeats);
static void hbtimer_delete (hbtimer_t *self);
static bool hbtimer_woke_up_at (const hbtimer_t *self, const struct timeval *tv);
static bool hbtimer_is_aligned_with (const hbtimer_t *self, const hbtimer_t *that);
static int hbtimer_common_wakeups (const hbtimer_t *self, const hbtimer_t *that);
/* -- req -- */
static void req_simultaneous(int *xc, const hbtimer_t *a, const hbtimer_t *b);
static void req_in_range (int *xc, const hbtimer_t *a);
static void req_aligned (int *xc, const hbtimer_t *a, const hbtimer_t *b);
static void req_common (int *xc, const hbtimer_t *a, const hbtimer_t *b, int n);
static void req_period (int *xc, const hbtimer_t *a, int n);
static void req_wakeups (int *xc, const hbtimer_t *a);
static void req_finishes (int *xc, const hbtimer_t *a, int n);
/* -- failure -- */
static gboolean failure_cb(gpointer user_data);
/* -- slots -- */
static void slots_test(int *xc);
/* -- range -- */
static void ranges_test(int *xc);
/* -- keepalive -- */
static void keepalive_start_cb(hbtimer_t *self);
static void keepalive_renew_cb(hbtimer_t *self);
static void keepalive_stop_cb (hbtimer_t *self);
static void keepalive_test (int *xc);
/* ------------------------------------------------------------------------- *
* struct timeval helpers
* ------------------------------------------------------------------------- */
static void tv_get_monotime(struct timeval *tv)
{
#if USE_MONOTONIC_TIME
/* CLOCK_MONOTONIC might not advance while device is suspended
* which makes it not ideal for timing waking up from suspend ... */
struct timespec ts;
if( clock_gettime(CLOCK_MONOTONIC, &ts) < 0 )
{
abort();
}
TIMESPEC_TO_TIMEVAL(tv, &ts);
#else
/* The system changes will cause tests to fail */
gettimeofday(tv, 0);
#endif
}
static int tv_diff_in_ms(const struct timeval *tv1, const struct timeval *tv2)
{
struct timeval tv;
timersub(tv1, tv2, &tv);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
/* ------------------------------------------------------------------------- *
* logging
* ------------------------------------------------------------------------- */
static void log_emit(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
static void log_emit(const char *fmt, ...)
{
static struct timeval tv0 = {0,0};
va_list va;
struct timeval tv;
tv_get_monotime(&tv);
if( !timerisset(&tv0) )
{
tv0 = tv;
}
timersub(&tv,&tv0,&tv);
fprintf(stderr, "<%03ld.%03ld> ", (long)tv.tv_sec, (long)(tv.tv_usec/1000));
va_start(va, fmt);
vfprintf(stderr, fmt, va);
va_end(va);
}
#define log_error( FMT,ARGS...) log_emit("E: "FMT"\n", ## ARGS)
#define log_warning(FMT,ARGS...) log_emit("W: "FMT"\n", ## ARGS)
/* for test results */
#define log_notice( FMT,ARGS...) log_emit("N: "FMT"\n", ## ARGS)
/* for progress reporting */
#define log_info( FMT,ARGS...) log_emit("I: "FMT"\n", ## ARGS)
/* normally we do not want to see these */
#if 0
# define log_debug( FMT,ARGS...) log_emit("D: "FMT"\n", ## ARGS)
#else
# define log_debug( FMT,ARGS...) do {} while(0)
#endif
/* ------------------------------------------------------------------------- *
* mainloop
* ------------------------------------------------------------------------- */
static GMainLoop *mainloop_handle = 0;
static int mainloop_status = EXIT_SUCCESS;
static void mainloop_stop(int exit_code)
{
log_info("@ %s(%d)", __FUNCTION__, exit_code);
if( mainloop_status < exit_code )
{
mainloop_status = exit_code;
}
if( !mainloop_handle )
{
exit(mainloop_status);
}
g_main_loop_quit(mainloop_handle);
}
static int mainloop_run(void)
{
log_info("@ %s()", __FUNCTION__);
if( (mainloop_handle = g_main_loop_new(0, 0)) )
{
g_main_loop_run(mainloop_handle);
g_main_loop_unref(mainloop_handle), mainloop_handle = 0;
}
log_info("@ %s() -> %d", __FUNCTION__, mainloop_status);
return mainloop_status;
}
/* ------------------------------------------------------------------------- *
* systembus
* ------------------------------------------------------------------------- */
static DBusConnection *systembus = 0;
static bool systembus_connect(void)
{
log_debug("@ %s()", __FUNCTION__);
DBusError err = DBUS_ERROR_INIT;
if( !(systembus = dbus_bus_get(DBUS_BUS_SYSTEM, &err)) )
{
log_error("can't connect to systembus: %s: %s",
err.name, err.message);
goto cleanup;
}
dbus_connection_setup_with_g_main(systembus, 0);
cleanup:
dbus_error_free(&err);
return systembus != 0;
}
static void systembus_disconnect(void)
{
if( systembus ) {
log_debug("@ %s()", __FUNCTION__);
dbus_connection_unref(systembus), systembus = 0;
}
}
/* ------------------------------------------------------------------------- *
* ipc with mce
* ------------------------------------------------------------------------- */
static int xmce_cpu_keepalive_period(void)
{
log_debug("@ %s()", __FUNCTION__);
int res = -1;
DBusMessage *req = 0;
DBusMessage *rsp = 0;
DBusError err = DBUS_ERROR_INIT;
dbus_int32_t dta = -1;
if( !systembus ) goto cleanup;
req = dbus_message_new_method_call(MCE_SERVICE,
MCE_REQUEST_PATH,
MCE_REQUEST_IF,
MCE_CPU_KEEPALIVE_PERIOD_REQ);
if( !req ) goto cleanup;
dbus_message_set_auto_start(req, false);
rsp = dbus_connection_send_with_reply_and_block(systembus, req, -1, &err);
if( !rsp )
{
log_error("failed to call %s.%s", MCE_REQUEST_IF, MCE_CPU_KEEPALIVE_PERIOD_REQ);
log_error("%s: %s", err.name, err.message);
goto cleanup;
}
if( dbus_set_error_from_message(&err, rsp) ||
!dbus_message_get_args(rsp, &err,
DBUS_TYPE_INT32, &dta,
DBUS_TYPE_INVALID) ) {
log_error("error reply to %s.%s", MCE_REQUEST_IF, MCE_CPU_KEEPALIVE_PERIOD_REQ);
log_error("%s: %s", err.name, err.message);
goto cleanup;
}
res = dta;
cleanup:
dbus_error_free(&err);
if( rsp ) dbus_message_unref(rsp);
if( req ) dbus_message_unref(req);
return res;
}
static bool xmce_method_call(const char *method)
{
log_debug("@ %s(%s)", __FUNCTION__, method);
bool res = false;
DBusMessage *req = 0;
if( !systembus ) goto cleanup;
req = dbus_message_new_method_call(MCE_SERVICE,
MCE_REQUEST_PATH,
MCE_REQUEST_IF,
method);
if( !req ) goto cleanup;
dbus_message_set_auto_start(req, false);
dbus_message_set_no_reply(req, true);
if( !dbus_connection_send(systembus, req, 0) ) {
log_error("failed to send %s.%s", MCE_REQUEST_IF, method);
goto cleanup;
}
res = true;
cleanup:
if( req ) dbus_message_unref(req);
return res;
}
static bool xmce_cpu_keepalive_start(void)
{
log_info("@ %s()", __FUNCTION__);
return xmce_method_call(MCE_CPU_KEEPALIVE_START_REQ);
}
static bool xmce_cpu_keepalive_stop(void)
{
log_info("@ %s()", __FUNCTION__);
return xmce_method_call(MCE_CPU_KEEPALIVE_STOP_REQ);
}
#ifdef DEAD_CODE
static bool xmce_display_on(void)
{
log_debug("@ %s()", __FUNCTION__);
return xmce_method_call(MCE_DISPLAY_ON_REQ);
}
static bool xmce_display_off(void)
{
log_debug("@ %s()", __FUNCTION__);
return xmce_method_call(MCE_DISPLAY_OFF_REQ);
}
#endif
/* ------------------------------------------------------------------------- *
* structure for controlling iphb timer & collecting wakeup statistics
* ------------------------------------------------------------------------- */
enum
{
HBTIMER_MAX_WAKEUPS = 16,
};
struct hbtimer_t
{
// unique identifier
int id;
// timer create time
struct timeval created;
// wakeup range
int mintime;
int maxtime;
// number of times to repeat sleep
int repeats;
// is the timer active?
bool active;
// connection to server side
iphb_t iphb_hnd;
// io watch for iphb_hnd
guint iphb_id;
// wakeup started hook
void (*start_cb) (hbtimer_t *self);
// renew hook
void (*renew_cb) (hbtimer_t *self);
int renew_time;
guint renew_id;
// finish hook
void (*finish_cb)(hbtimer_t *self);
int finish_time;
guint finish_id;
// wakeup started stats
int wakeups;
struct timeval wakeup[HBTIMER_MAX_WAKEUPS];
// wakeup finished stats
int finishes;
struct timeval finish[HBTIMER_MAX_WAKEUPS];
// NB: if changed, adjust hbtimer_ctor() and hbtimer_dtor() too
};
static int active_count = 0;
static void hbtimer_show_stats(const hbtimer_t *self)
{
log_notice("statistics for timer %d:", self->id);
log_notice(" range %d-%d", self->mintime, self->maxtime);
log_notice(" wakeups %d/%d", self->wakeups, self->repeats);
log_notice(" finishes %d/%d", self->finishes, self->repeats);
for( int i = 0; i < self->repeats; ++i )
{
struct timeval b = {0,0};
struct timeval e = {0,0};
struct timeval d = {0,0};
if( i < self->wakeups )
{
timersub(&self->wakeup[i], &self->created, &b);
if( i < self->finishes )
{
timersub(&self->finish[i], &self->created, &e);
timersub(&e, &b, &d);
}
}
log_notice(" %2d: %7.3f .. %7.3f = %7.3f", i+1,
b.tv_sec + b.tv_usec * 1e-6,
e.tv_sec + e.tv_usec * 1e-6,
d.tv_sec + d.tv_usec * 1e-6);
}
}
static gboolean hbtimer_handle_wakeup_renew_cb(gpointer aptr)
{
hbtimer_t *self = aptr;
if( self->renew_cb )
{
self->renew_cb(self);
}
return TRUE;
}
static gboolean hbtimer_handle_wakeup_finish_cb(gpointer aptr)
{
hbtimer_t *self = aptr;
self->finish_id = 0;
if( self->finish_cb )
{
self->finish_cb(self);
}
hbtimer_handle_wakeup_finish(self);
return FALSE;
}
static void hbtimer_cancel_timers(hbtimer_t *self)
{
if( self->renew_id )
{
g_source_remove(self->renew_id), self->renew_id = 0;
}
if( self->finish_id )
{
g_source_remove(self->finish_id), self->finish_id = 0;
}
}
static void hbtimer_start_timers(hbtimer_t *self)
{
if( !self->finish_id && self->finish_time > 0 )
{
self->finish_id = g_timeout_add_seconds(self->finish_time,
hbtimer_handle_wakeup_finish_cb,
self);
}
if( !self->renew_id && self->renew_time > 0 )
{
self->renew_id = g_timeout_add_seconds(self->renew_time,
hbtimer_handle_wakeup_renew_cb,
self);
}
}
static void hbtimer_set_start(hbtimer_t *self, void (*cb)(hbtimer_t *))
{
self->start_cb = cb;
}
static void hbtimer_set_finish(hbtimer_t *self, void (*cb)(hbtimer_t *), int secs)
{
self->finish_cb = cb;
self->finish_time = secs;
}
static void hbtimer_set_renew(hbtimer_t *self, void (*cb)(hbtimer_t *), int secs)
{
self->renew_cb = cb;
self->renew_time = secs;
}
static bool hbtimer_deactivate(hbtimer_t *self)
{
bool was_last = false;
if( self->active )
{
self->active = false;
if( --active_count == 0 )
{
was_last = true;
}
}
return was_last;
}
static void hbtimer_activate(hbtimer_t *self)
{
if( !self->active )
{
self->active = true;
++active_count;
}
}
static void hbtimer_handle_wakeup_finish(hbtimer_t *self)
{
log_debug("@ %s(%d)", __FUNCTION__, self->id);
if( self->finishes == HBTIMER_MAX_WAKEUPS )
{
abort();
}
hbtimer_cancel_timers(self);
tv_get_monotime(&self->finish[self->finishes++]);
if( self->finishes < self->repeats )
{
// nop
}
else if( hbtimer_deactivate(self) )
{
mainloop_stop(0);
}
}
static void hbtimer_handle_wakeup_start(hbtimer_t *self)
{
log_debug("@ %s(%d)", __FUNCTION__, self->id);
if( self->wakeups == HBTIMER_MAX_WAKEUPS )
{
abort();
}
tv_get_monotime(&self->wakeup[self->wakeups++]);
if( self->wakeups < self->repeats )
{
hbtimer_start(self);
}
if( self->start_cb )
{
self->start_cb(self);
hbtimer_start_timers(self);
}
if( !self->finish_id )
{
hbtimer_handle_wakeup_finish(self);
}
}
static
gboolean
hbtimer_wakeup_cb(GIOChannel* chan, GIOCondition condition, gpointer data)
{
hbtimer_t *self = data;
log_info("@ %s(%d)", __FUNCTION__, self->id);
gboolean keep_going = TRUE;
char buf[256];
/* Abandon watch if we get abnormal conditions from glib */
if (condition & ~(G_IO_IN | G_IO_PRI))
{
log_error("unexpected io watch condition");
keep_going = FALSE;
}
/* Read the data to clear input available state */
int fd = g_io_channel_unix_get_fd(chan);
int rc = read(fd, buf, sizeof buf);
if( rc < 0 )
{
switch( errno )
{
case EINTR:
case EAGAIN:
break;
default:
log_error("io watch read: %m");
keep_going = FALSE;
break;
}
}
else if( rc == 0 )
{
log_error("io watch read: EOF");
keep_going = FALSE;
}
else
{
hbtimer_handle_wakeup_start(self);
}
/* Clear the timer id if we're going to stop */
if( !keep_going )
{
log_error("io failure, disabling io watch");
self->iphb_id = 0;
}
return keep_going;
}
static void hbtimer_disconnect(hbtimer_t *self)
{
log_debug("@ %s(%d)", __FUNCTION__, self->id);
if( self->iphb_id )
{
g_source_remove(self->iphb_id), self->iphb_id = 0;
}
if( self->iphb_hnd )
{
iphb_close(self->iphb_hnd), self->iphb_hnd = 0;
}
}
static bool hbtimer_connect(hbtimer_t *self)
{
int file = -1;
GIOChannel *chan = 0;
GError *err = 0;
if( self->iphb_hnd )
{
goto cleanup;
}
log_debug("@ %s(%d)", __FUNCTION__, self->id);
if( !(self->iphb_hnd = iphb_open(0)) )
{
goto cleanup;
}
if( (file = iphb_get_fd(self->iphb_hnd)) == -1 )
{
goto cleanup;
}
if( !(chan = g_io_channel_unix_new(file)) )
{
log_error("creating io channel failed");
goto cleanup;
}
/* iphb_hnd "owns" the file descriptor */
g_io_channel_set_close_on_unref(chan, false);
/* Set to NULL encoding so that we can turn off the buffering */
if( g_io_channel_set_encoding(chan, NULL, &err) != G_IO_STATUS_NORMAL )
{
log_warning("failed to set io channel encoding: %s",
err ? err->message : "unknown reason");
// try to continue anyway
}
g_io_channel_set_buffered(chan, false);
self->iphb_id = g_io_add_watch(chan, G_IO_IN, hbtimer_wakeup_cb, self);
if( !self->iphb_id )
{
log_error("failed to add io channel watch");
goto cleanup;
}
cleanup:
g_clear_error(&err);
if( chan ) g_io_channel_unref(chan);
return self->iphb_hnd && self->iphb_id;
}
static bool hbtimer_start(hbtimer_t *self)
{
log_debug("@ %s(%d)", __FUNCTION__, self->id);
bool res = false;
if( !hbtimer_connect(self) )
{
goto cleanup;
}
if( iphb_wait2(self->iphb_hnd, self->mintime, self->maxtime, 0, 1) < 0 )
{
goto cleanup;
}
res = true;
cleanup:
return res;
}
static void hbtimer_ctor(hbtimer_t *self)
{
static int id = 0;
tv_get_monotime(&self->created);
self->id = ++id;
log_debug("@ %s(%d)", __FUNCTION__, self->id);
self->mintime = 0;
self->maxtime = 0;
self->repeats = 0;
self->active = false;
self->iphb_hnd = 0;
self->iphb_id = 0;
self->start_cb = 0;
self->renew_cb = 0;
self->finish_cb = 0;
self->renew_time = 0;
self->finish_time = 0;
self->renew_id = 0;
self->finish_id = 0;
self->wakeups = 0;
self->finishes = 0;
}
static void hbtimer_dtor(hbtimer_t *self)
{
log_debug("@ %s(%d)", __FUNCTION__, self->id);
hbtimer_cancel_timers(self);
hbtimer_deactivate(self);
hbtimer_disconnect(self);
}
static void hbtimer_delete(hbtimer_t *self)
{
if( self != 0 )
{
hbtimer_dtor(self);
free(self);
}
}
static void hbtimer_setup(hbtimer_t *self, int mintime, int maxtime, int repeats)
{
log_info("@ %s(%d, %d, %d, %d)", __FUNCTION__, self->id, mintime, maxtime, repeats);
self->mintime = mintime;
self->maxtime = maxtime;
self->repeats = repeats;
if( repeats > 0 )
{
hbtimer_activate(self);
hbtimer_start(self);
}
}
static hbtimer_t *hbtimer_create(int mintime, int maxtime, int repeats)
{
hbtimer_t *self = calloc(1, sizeof *self);
hbtimer_ctor(self);
hbtimer_setup(self, mintime, maxtime, repeats);
return self;
}
static bool hbtimer_woke_up_at(const hbtimer_t *self, const struct timeval *tv)
{
for( int i = 0; i < self->wakeups; ++i )
{
int ms = tv_diff_in_ms(&self->wakeup[i], tv);
if( abs(ms) < 100 )
{
return true;
}
}
return false;
}
static bool hbtimer_is_aligned_with(const hbtimer_t *self, const hbtimer_t *that)
{
for( int i = 0; i < self->wakeups; ++i )
{
if( !hbtimer_woke_up_at(that, &self->wakeup[i]) )
{
return false;
}
}
return true;
}
static int hbtimer_common_wakeups(const hbtimer_t *self, const hbtimer_t *that)
{
int res = 0;
for( int i = 0; i < self->wakeups; ++i )
{
if( hbtimer_woke_up_at(that, &self->wakeup[i]) )
{
++res;
}
}
return res;
}
/* ------------------------------------------------------------------------- *
* helpers for checking if requirements are met
* ------------------------------------------------------------------------- */
static void req_simultaneous(int *xc, const hbtimer_t *a, const hbtimer_t *b)
{
if( !hbtimer_is_aligned_with(a, b) || !hbtimer_is_aligned_with(b,a) )
{
log_error("timer %d and timer %d wakeups were not simultaneous", a->id, b->id);
*xc = EXIT_FAILURE;
}
}
static void req_in_range(int *xc, const hbtimer_t *a)
{
if( a->wakeups )
{
int lo = a->mintime * 1000;
int hi = a->maxtime * 1000;
int ms = tv_diff_in_ms(&a->wakeup[0], &a->created);
if( ms < lo || ms > hi + ALLOWED_DELAY )
{
log_error("timer %d wait time %d ms out of range %d - %d ms", a->id, ms, lo, hi);
*xc = EXIT_FAILURE;
}
}
}
static void req_aligned(int *xc, const hbtimer_t *a, const hbtimer_t *b)
{
if( !hbtimer_is_aligned_with(a, b) )
{
log_error("timer %d wakeups were not aligned with timer %d", a->id, b->id);
*xc = EXIT_FAILURE;
}
}
static void req_common(int *xc, const hbtimer_t *a, const hbtimer_t *b, int n)
{
int m = hbtimer_common_wakeups(a, b);
if( m != n )
{
log_error("common wakeups for %d and %d: %d, expected %d",
a->id, b->id, m, n);
*xc = EXIT_FAILURE;
}
}
static void req_period(int *xc, const hbtimer_t *a, int n)
{
n *= 1000;
for( int i = 1; i < a->wakeups; ++i )
{
int ms = tv_diff_in_ms(&a->wakeup[i], &a->wakeup[i-1]);
if( abs(ms - n) > ALLOWED_DELAY )
{
log_error("timer %d, wakeup %d is %d ms, expected %d", a->id, i+1, ms, n);
*xc = EXIT_FAILURE;
}
}
}
static void req_wakeups(int *xc, const hbtimer_t *a)
{
if( a->wakeups != a->repeats )
{
log_error("timer %d woke up %d times, expected %d times",
a->id, a->wakeups, a->repeats);
*xc = EXIT_FAILURE;
}
}
static void req_finishes(int *xc, const hbtimer_t *a, int n)
{
if( a->finishes != a->repeats )
{
log_error("worktime %d finished %d times, expected %d times",
a->id, a->finishes, a->repeats);
*xc = EXIT_FAILURE;
}
if( a->finishes != a->wakeups )
{
log_error("worktime %d started %d times, but finished %d times",
a->id, a->wakeups, a->finishes);
*xc = EXIT_FAILURE;
}
n *= 1000;
for( int i = 0; i < a->finishes && i < a->wakeups; ++i )
{
int ms = tv_diff_in_ms(&a->finish[i], &a->wakeup[i]);
if( abs(ms - n) > ALLOWED_DELAY )
{
log_error("timer %d, worktime %d is %d ms, expected %d", a->id, i+1, ms, n);
*xc = EXIT_FAILURE;
}
}
}
/* ------------------------------------------------------------------------- *
* test is taking too long timeout
* ------------------------------------------------------------------------- */
static gboolean failure_cb(gpointer user_data)
{
log_error("test case did not finish in time");
mainloop_stop(EXIT_FAILURE);
return *(guint *)user_data = 0, FALSE;
}
/* ------------------------------------------------------------------------- *
* test global wakeup slot triggering
* ------------------------------------------------------------------------- */
static void slots_test(int *xc)
{
log_notice("testing global wakeup slots");
hbtimer_t *timer[16] = {};
int timers = 0;
guint timeout = 0;