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