On Sat, 2 Nov 2019 12:33:58 -0700 Michael Forney <mfor...@mforney.org> wrote:
> I've never used vmstat before, but this looks pretty good overall and > seems to work well. > > On 2019-10-05, Mattias Andrée <maand...@kth.se> wrote: > > + goto beginning; > > + for (; argc && (argc < 2 || i < count); i++) { > > Why not just set count = 1 when argc < 2? Because that would stop the loop after 1 iteration. If argc == 1, the loop should be infinite. > > > + clock_nanosleep(CLOCK_MONOTONIC, 0, &delay, NULL); > > + beginning: > > + load_vm(&vm[i & 1]); > > + print_vm(&vm[i & 1], &vm[~i & 1], active_mem, timestamp, > > one_header ? !i > > : (i % 50 == 0)); > > + } > > I think it might be a bit clearer to re-arrange the loop body to avoid > the labels and goto. > > Something like > > count = argc == 2 ? atoll(argv[1]) : 1; > for (;;) { > load_vm(&vm[i & 1]); > print_vm(&vm[i & 1], &vm[~i & 1], active_mem, timestamp, > one_header > ? !i : (i % 50 == 0)); > if (++i == count) > break; > clock_nanosleep(CLOCK_MONOTONIC, 0, &delay, NULL); > } > This does not work as excepted if argc == 1, it would just run it once rather than forever. An alternative that would work is: for (;;) { load_vm(&vm[i & 1]); print_vm(&vm[i & 1], &vm[~i & 1], active_mem, timestamp, one_header ? !i : (i % 50 == 0)); i++; if (!argc || (argc == 2 && i == count)) break; clock_nanosleep(CLOCK_MONOTONIC, 0, &delay, NULL); } or (I prefer this one less): load_vm(&vm[i & 1]); print_vm(&vm[i & 1], &vm[~i & 1], active_mem, timestamp, one_header ? !i : (i % 50 == 0)); for (i++; argc && (argc < 2 || i < count); i++) { clock_nanosleep(CLOCK_MONOTONIC, 0, &delay, NULL); load_vm(&vm[i & 1]); print_vm(&vm[i & 1], &vm[~i & 1], active_mem, timestamp, one_header ? !i : (i % 50 == 0)); } (or this `argc &&` broken out as an if-statement) I prefer the original version.