]> rtime.felk.cvut.cz Git - frescor/streamer.git/blob - rt_qosmgr.c
Update
[frescor/streamer.git] / rt_qosmgr.c
1 #include <stdio.h>
2 #include <libavformat/avformat.h>
3
4 #define QOS_DEBUG_LEVEL QOS_LEVEL_DEBUG
5 #include <linux/aquosa/qos_debug.h>
6
7 #include <aquosa/qmgr_lib.h>
8 #include <aquosa/percpred_lib.h>
9 #include <aquosa/stochdb_lib.h>
10
11 extern int fps;
12
13 #define SERV_PERIOD 10000lu     /* us */
14 #define TASK_PERIOD 40000lu     /* us */
15 #define SAMPLE_SZ 3
16 #define DISCARDED 1     /* discard 1 out of 3 ==> percentile = 66.66% */
17
18 static uint64_t start_time, start_time1;
19 static uint64_t thebeginning;
20 static int cnt;
21
22 void rt_job_start(uint64_t t)
23 {
24   static int started = 0;
25   struct timespec now, now1;
26   static int64_t delta;
27   int64_t dl;
28
29 #if 1
30   t += (1000000ul / fps);       /* Hack to work around v4l2vd bug... */
31 #endif
32
33   if (! started) {
34     thebeginning = t;
35     started = 1;
36     qos_log_debug("Initializing qmgr library\n");
37     qos_chk_ok_exit(qmgr_init());
38
39     qos_log_debug("Setting predictor\n");
40     /* Defaults to 3 samples and forgetting factor = 1 */
41     qos_chk_ok_exit(qmgr_set_predictor(QMGR_PRED_PERC));
42
43     qos_log_debug("Setting sample size to %d\n", SAMPLE_SZ);
44     /* Sets sample size */
45     qos_chk_ok_exit(percpred_set_sample_size(SAMPLE_SZ));
46
47     qos_log_debug("Setting discarded to %d\n", DISCARDED);
48     /* Sets discarded */
49     qos_chk_ok_exit(percpred_set_discarded(DISCARDED));
50
51     qos_log_debug("Setting controller\n");
52     /* Defaults to null target s.e. */
53     //qos_chk_ok_exit(qmgr_set_controller(QMGR_CTRL_INVARIANT/*STOCHDB*/));
54     qos_chk_ok_exit(qmgr_set_controller(QMGR_CTRL_STOCHDB));
55
56     qos_log_debug("Setting controller parameters\n");
57     /* Assign task period and granularity */
58     qmgr_set_task_period(1000000 / fps);
59     qmgr_set_server_period(1000000 / fps / 6);
60     qmgr_set_max_bandwidth(0.95);
61
62     qos_log_debug("Starting control loop");
63     qos_chk_ok_exit(qmgr_start());
64     clock_gettime(CLOCK_MONOTONIC, &now);
65     clock_gettime(CLOCK_REALTIME, &now1);
66     delta = (now1.tv_sec - now.tv_sec) * 1000000ll + (now1.tv_nsec - now.tv_nsec) / 1000ll;
67   }
68   dl = t + (1000000ul / fps);
69   dl -= delta;
70   now.tv_nsec = (dl % 1000000) * 1000;
71   now.tv_sec = dl / 1000000;
72   qmgr_begin_cycle_deadline(&now);
73   start_time = t;
74   start_time1 = av_gettime();
75 }
76
77 void rt_job_end(void)
78 {
79   uint64_t t;
80   qres_time_t sched_err, etime;
81
82   t = av_gettime();
83   printf("%lu %lu %lu\t%lu %lu\t", start_time, start_time1, t, t - start_time, t - start_time1);
84   if (cnt == 0) printf("0\t");
85   else printf("%lu\t", (start_time - thebeginning) / cnt);
86   cnt++;
87   qmgr_end_cycle(&sched_err, &etime);
88   printf(QRES_TIME_FMT "\n", sched_err);
89   //qos_log_debug("Ending job: " QRES_TIME_FMT "\n", sched_err);
90 }