Skip to content

Commit 395af10

Browse files
committed
cli CHANGE commands can now be timed
...using new timed command.
1 parent 917eb39 commit 395af10

File tree

1 file changed

+98
-1
lines changed

1 file changed

+98
-1
lines changed

cli/commands.c

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ char *config_editor;
6868
struct nc_session *session;
6969
volatile pthread_t ntf_tid;
7070
volatile int interleave;
71+
int timed;
7172

7273
int cmd_disconnect(const char *arg, char **tmp_config_file);
7374

@@ -228,18 +229,70 @@ cli_ntf_clb(struct nc_session *session, const struct nc_notif *notif)
228229
}
229230
}
230231

232+
static int
233+
cli_gettimespec(struct timespec *ts, int *mono)
234+
{
235+
errno = 0;
236+
237+
#ifdef CLOCK_MONOTONIC_RAW
238+
*mono = 1;
239+
return clock_gettime(CLOCK_MONOTONIC_RAW, ts);
240+
#elif defined(CLOCK_MONOTONIC)
241+
*mono = 1;
242+
return clock_gettime(CLOCK_MONOTONIC, ts);
243+
#elif defined(CLOCK_REALTIME)
244+
/* no monotonic clock available, return realtime */
245+
*mono = 0;
246+
return clock_gettime(CLOCK_REALTIME, ts);
247+
#else
248+
*mono = 0;
249+
250+
int rc;
251+
struct timeval tv;
252+
253+
rc = gettimeofday(&tv, NULL);
254+
if (!rc) {
255+
ts->tv_sec = (time_t)tv.tv_sec;
256+
ts->tv_nsec = 1000L * (long)tv.tv_usec;
257+
}
258+
return rc;
259+
#endif
260+
}
261+
262+
/* returns milliseconds */
263+
static int32_t
264+
cli_difftimespec(const struct timespec *ts1, const struct timespec *ts2)
265+
{
266+
int64_t nsec_diff = 0;
267+
268+
nsec_diff += (((int64_t)ts2->tv_sec) - ((int64_t)ts1->tv_sec)) * 1000000000L;
269+
nsec_diff += ((int64_t)ts2->tv_nsec) - ((int64_t)ts1->tv_nsec);
270+
271+
return (nsec_diff ? nsec_diff / 1000000L : 0);
272+
}
273+
231274
static int
232275
cli_send_recv(struct nc_rpc *rpc, FILE *output, NC_WD_MODE wd_mode)
233276
{
234277
char *str, *model_data;
235-
int ret = 0, ly_wd;
278+
int ret = 0, ly_wd, mono;
279+
int32_t msec;
236280
uint16_t i, j;
237281
uint64_t msgid;
238282
struct lyd_node_anydata *any;
239283
NC_MSG_TYPE msgtype;
240284
struct nc_reply *reply;
241285
struct nc_reply_data *data_rpl;
242286
struct nc_reply_error *error;
287+
struct timespec ts_start, ts_stop;
288+
289+
if (timed) {
290+
ret = cli_gettimespec(&ts_start, &mono);
291+
if (ret) {
292+
ERROR(__func__, "Getting current time failed (%s).", strerror(errno));
293+
return ret;
294+
}
295+
}
243296

244297
msgtype = nc_send_rpc(session, rpc, 1000, &msgid);
245298
if (msgtype == NC_MSG_ERROR) {
@@ -275,6 +328,15 @@ cli_send_recv(struct nc_rpc *rpc, FILE *output, NC_WD_MODE wd_mode)
275328
goto recv_reply;
276329
}
277330

331+
if (timed) {
332+
ret = cli_gettimespec(&ts_stop, &mono);
333+
if (ret) {
334+
ERROR(__func__, "Getting current time failed (%s).", strerror(errno));
335+
nc_reply_free(reply);
336+
return ret;
337+
}
338+
}
339+
278340
switch (reply->type) {
279341
case NC_RPL_OK:
280342
fprintf(output, "OK\n");
@@ -430,6 +492,11 @@ cli_send_recv(struct nc_rpc *rpc, FILE *output, NC_WD_MODE wd_mode)
430492
goto recv_reply;
431493
}
432494

495+
if (timed) {
496+
msec = cli_difftimespec(&ts_start, &ts_stop);
497+
fprintf(output, "%s %2dm%d,%03ds\n", mono ? "mono" : "real", msec / 60000, (msec % 60000) / 1000, msec % 1000);
498+
}
499+
433500
return ret;
434501
}
435502

@@ -1012,6 +1079,12 @@ cmd_userrpc_help(void)
10121079
printf("user-rpc [--help] [--content <file>] [--out <file>]\n");
10131080
}
10141081

1082+
void
1083+
cmd_timed_help(void)
1084+
{
1085+
printf("timed [--help] [on | off]\n");
1086+
}
1087+
10151088
#ifdef NC_ENABLED_SSH
10161089

10171090
void
@@ -4538,6 +4611,29 @@ cmd_userrpc(const char *arg, char **tmp_config_file)
45384611
return ret;
45394612
}
45404613

4614+
int
4615+
cmd_timed(const char *arg, char **UNUSED(tmp_config_file))
4616+
{
4617+
char *args = strdupa(arg);
4618+
char *cmd = NULL;
4619+
4620+
strtok(args, " ");
4621+
if ((cmd = strtok(NULL, " ")) == NULL) {
4622+
fprintf(stdout, "All commands will %sbe timed.\n", timed ? "" : "not ");
4623+
} else {
4624+
if (!strcmp(cmd, "on")) {
4625+
timed = 1;
4626+
} else if (!strcmp(cmd, "off")) {
4627+
timed = 0;
4628+
} else {
4629+
ERROR(__func__, "Unknown option %s.", cmd);
4630+
cmd_timed_help();
4631+
}
4632+
}
4633+
4634+
return 0;
4635+
}
4636+
45414637
COMMAND commands[] = {
45424638
#ifdef NC_ENABLED_SSH
45434639
{"auth", cmd_auth, cmd_auth_help, "Manage SSH authentication options"},
@@ -4573,6 +4669,7 @@ COMMAND commands[] = {
45734669
{"subscribe", cmd_subscribe, cmd_subscribe_help, "notifications <create-subscription> operation"},
45744670
{"get-schema", cmd_getschema, cmd_getschema_help, "ietf-netconf-monitoring <get-schema> operation"},
45754671
{"user-rpc", cmd_userrpc, cmd_userrpc_help, "Send your own content in an RPC envelope (for DEBUG purposes)"},
4672+
{"timed", cmd_timed, cmd_timed_help, "Time all the commands (that communicate with a server) from issuing a RPC to getting a reply"},
45764673
/* synonyms for previous commands */
45774674
{"?", cmd_help, NULL, "Display commands description"},
45784675
{"exit", cmd_quit, NULL, "Quit the program"},

0 commit comments

Comments
 (0)