51 #include <iothinx/iothinxio.h> 54 int set_process_priority(
int pid,
int priority_level)
58 struct sched_param param = { 0 };
60 switch (priority_level)
64 param.sched_priority = 1;
68 param.sched_priority = 49;
72 param.sched_priority = 99;
77 param.sched_priority = 0;
80 rc = sched_setscheduler(pid, policy, ¶m);
83 perror(
"sched_setscheduler");
89 int main(
int argc,
char **
const argv)
93 uint8_t do_channel = 0;
95 uint32_t do_values = 0;
96 uint32_t test_cnt = 1000;
97 uint32_t cycle_time_us = 10000;
99 time_t start_time_us, end_time_us;
100 time_t last_start_time_us = 0;
101 int priority_level = 3;
107 while (-1 != (rc = getopt(argc, argv,
"c:hn:p:s:t:")))
112 do_channel = atoi(optarg);
115 test_cnt = atoi(optarg);
118 priority_level = atoi(optarg);
121 do_slot = atoi(optarg);
124 cycle_time_us = atoi(optarg);
128 printf(
"Scheduling jitter sample program.\n\n");
129 printf(
"Usage: ./scheduling_jitter [OPTIONS]\n\n");
130 printf(
"Options: \n");
131 printf(
"\t %-8s Priority level[0-3]. Default level = %d\n",
"-p", priority_level);
132 printf(
"\t %-8s Slot of DO module. Default slot = %d\n",
"-s", do_slot);
133 printf(
"\t %-8s DO channel. Default channel = %d\n",
"-c", do_channel);
134 printf(
"\t %-8s Testing loop count. Default count = %d\n",
"-n", test_cnt);
135 printf(
"\t %-8s Testing loop cycle time (in us). Default time = %d us\n",
"-t", cycle_time_us);
141 printf(
"Priority level = %d\n", priority_level);
142 printf(
"DO slot = %lu\n", do_slot);
143 printf(
"DO channel = %u\n", do_channel);
149 printf(
"ioThinx_IO_Client_Init() = %d\n", rc);
157 printf(
"ioThinx_DO_Config_SetModes() = %d\n", rc);
165 printf(
"ioThinx_IO_Config_Reload() = %d\n", rc);
170 set_process_priority(0, priority_level);
176 printf(
"Start testing ...\n");
178 for (i = 0; i < test_cnt; i++)
181 clock_gettime(CLOCK_MONOTONIC, &ts);
182 start_time_us = (ts.tv_sec * 1000000 + ts.tv_nsec / 1000);
183 diff_us = (last_start_time_us == 0) ? 0 : start_time_us - last_start_time_us - cycle_time_us;
185 max_us = (diff_us > max_us) ? diff_us : max_us;
186 last_start_time_us = start_time_us;
190 if (do_values & (0x1 << do_channel))
192 do_values &= ~(0x1 << do_channel);
196 do_values |= 0x1 << do_channel;
201 printf(
"ioThinx_DO_SetValues() = %d\n", rc);
206 clock_gettime(CLOCK_MONOTONIC, &ts);
207 end_time_us = (ts.tv_sec * 1000000 + ts.tv_nsec / 1000);
210 diff_us = cycle_time_us - (end_time_us - start_time_us);
212 ts.tv_nsec = diff_us * 1000;
215 clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
219 printf(
"Scheduling jitter: Avg. = %.1f us, Max = %ld us\n",
220 (
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[])
IOTHINX_ERR ioThinx_IO_Config_Reload(void)
int main(int argc, char **const argv)
IOTHINX_ERR ioThinx_IO_Client_Init(void)