52 #include <iothinx/iothinxio.h> 59 struct sched_param param = { 0 };
61 switch (priority_level)
65 param.sched_priority = 1;
69 param.sched_priority = 49;
73 param.sched_priority = 99;
78 param.sched_priority = 0;
81 rc = sched_setscheduler(pid, policy, ¶m);
84 perror(
"sched_setscheduler");
90 int main(
int argc,
char **
const argv)
94 uint8_t do_channel = 0;
96 uint32_t do_values = 0;
97 uint32_t test_cnt = 1000;
98 uint32_t cycle_time_us = 10000;
100 time_t start_time_us, end_time_us;
101 time_t last_start_time_us = 0;
102 int priority_level = 3;
108 while (-1 != (rc = getopt(argc, argv,
"c:hn:p:s:t:")))
113 do_channel = atoi(optarg);
116 test_cnt = atoi(optarg);
119 priority_level = atoi(optarg);
122 do_slot = atoi(optarg);
125 cycle_time_us = atoi(optarg);
129 printf(
"Scheduling jitter sample program.\n\n");
130 printf(
"Usage: ./scheduling_jitter [OPTIONS]\n\n");
131 printf(
"Options: \n");
132 printf(
"\t %-8s Priority level[0-3]. Default level = %d\n",
"-p", priority_level);
133 printf(
"\t %-8s Slot of DO module. Default slot = %d\n",
"-s", do_slot);
134 printf(
"\t %-8s DO channel. Default channel = %d\n",
"-c", do_channel);
135 printf(
"\t %-8s Testing loop count. Default count = %d\n",
"-n", test_cnt);
136 printf(
"\t %-8s Testing loop cycle time (in us). Default time = %d us\n",
"-t", cycle_time_us);
142 printf(
"Priority level = %d\n", priority_level);
143 printf(
"DO slot = %lu\n", do_slot);
144 printf(
"DO channel = %u\n", do_channel);
150 printf(
"ioThinx_IO_Client_Init() = %d\n", rc);
158 printf(
"ioThinx_DO_Config_SetModes() = %d\n", rc);
166 printf(
"ioThinx_IO_Config_Reload() = %d\n", rc);
177 printf(
"Start testing ...\n");
179 for (i = 0; i < test_cnt; i++)
182 clock_gettime(CLOCK_MONOTONIC, &ts);
183 start_time_us = (ts.tv_sec * 1000000 + ts.tv_nsec / 1000);
184 diff_us = (last_start_time_us == 0) ? 0 : start_time_us - last_start_time_us - cycle_time_us;
186 max_us = (diff_us > max_us) ? diff_us : max_us;
187 last_start_time_us = start_time_us;
191 if (do_values & (0x1 << do_channel))
193 do_values &= ~(0x1 << do_channel);
197 do_values |= 0x1 << do_channel;
202 printf(
"ioThinx_DO_SetValues() = %d\n", rc);
207 clock_gettime(CLOCK_MONOTONIC, &ts);
208 end_time_us = (ts.tv_sec * 1000000 + ts.tv_nsec / 1000);
211 diff_us = cycle_time_us - (end_time_us - start_time_us);
213 ts.tv_nsec = diff_us * 1000;
216 clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
220 printf(
"Scheduling jitter: Avg. = %.1f us, Max = %ld us\n",
221 (
float)sum_us / (
float)(test_cnt - 1), max_us);
IOTHINX_ERR ioThinx_DO_SetValues(uint32_t slot, uint32_t values)
IOTHINX_ERR ioThinx_DO_Config_SetModes(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
int main(int argc, char **const argv)
IOTHINX_ERR ioThinx_IO_Config_Reload(void)
int set_process_priority(int pid, int priority_level)
IOTHINX_ERR ioThinx_IO_Client_Init(void)