Skip to content

Instantly share code, notes, and snippets.

@JC3
Created November 3, 2019 22:53
Show Gist options
  • Save JC3/fe3780cdfe609009e43ca14192a277eb to your computer and use it in GitHub Desktop.
Save JC3/fe3780cdfe609009e43ca14192a277eb to your computer and use it in GitHub Desktop.
x sync extension system counter list and test
// link to -lX11 -lXext
#include <cstdio>
#include <X11/Xlib.h>
#include <X11/extensions/sync.h>
#include <inttypes.h>
#include <sys/time.h>
#include <unistd.h>
static void testCounterFrequency (Display *display, XSyncSystemCounter *sc) {
XSyncValue xvstart, xvend;
printf("%s... ", sc->name);
fflush(stdout);
{
XSyncValue xvinit;
XSyncQueryCounter(display, sc->counter, &xvinit);
do {
XSyncQueryCounter(display, sc->counter, &xvstart);
} while (XSyncValueEqual(xvstart, xvinit));
}
timeval tvstart, tvend;
gettimeofday(&tvstart, NULL);
usleep(500000);
{
XSyncValue xvinit;
XSyncQueryCounter(display, sc->counter, &xvinit);
do {
XSyncQueryCounter(display, sc->counter, &xvend);
} while (XSyncValueEqual(xvend, xvinit));
}
gettimeofday(&tvend, NULL);
uint64_t xsstart = ((uint64_t)XSyncValueHigh32(xvstart) << 32) | XSyncValueLow32(xvstart);
uint64_t xsend = ((uint64_t)XSyncValueHigh32(xvend) << 32) | XSyncValueLow32(xvend);
double frequency =
(xsend - xsstart) /
((tvend.tv_sec - tvstart.tv_sec) +
(tvend.tv_usec - tvstart.tv_usec) / 1000000.0);
printf("%.6f\n", frequency);
}
int main (int argc, const char **argv) {
const char *displayName = (argc > 1) ? argv[1] : ":0";
Display *display = XOpenDisplay(displayName);
if (!display) {
fprintf(stderr, "failed to open display %s\n", displayName);
return 1;
}
int eventBase, errorBase;
if (!XSyncQueryExtension(display, &eventBase, &errorBase)) {
fprintf(stderr, "xsync extension not supported\n");
return 1;
} else {
printf("eventBase=%d\nerrorBase=%d\n", eventBase, errorBase);
}
int syncMajor, syncMinor;
if (!XSyncInitialize(display, &syncMajor, &syncMinor)) {
fprintf(stderr, "xsync initialization failed\n");
return 1;
} else {
printf("xsync version %d.%d\n", syncMajor, syncMinor);
}
XSyncSystemCounter *counterList;
int numCounters;
if (!(counterList = XSyncListSystemCounters(display, &numCounters))) {
fprintf(stderr, "failed to get xsync counter list\n");
return 1;
}
printf("system counters: %d\n", numCounters);
for (int n = 0; n < numCounters; ++ n) {
const XSyncSystemCounter *counter = counterList + n;
uint64_t resolution = ((uint64_t)XSyncValueHigh32(counter->resolution) << 32) | XSyncValueLow32(counter->resolution);
printf(" [%d] \"%s\" resolution=%lu\n", n, counter->name, resolution);
}
printf("testing system counter frequencies...\n");
for (int n = 0; n < numCounters; ++ n)
testCounterFrequency(display, counterList + n);
XSyncFreeSystemCounterList(counterList);
XCloseDisplay(display);
}
@JC3
Copy link
Author

JC3 commented Nov 3, 2019

Output on my machine:

eventBase=83
errorBase=134
xsync version 3.1
system counters: 17
  [0] "DEVICEIDLETIME 12" resolution=4
  [1] "DEVICEIDLETIME 11" resolution=4
  [2] "DEVICEIDLETIME 16" resolution=4
  [3] "DEVICEIDLETIME 15" resolution=4
  [4] "DEVICEIDLETIME 14" resolution=4
  [5] "DEVICEIDLETIME 13" resolution=4
  [6] "DEVICEIDLETIME 10" resolution=4
  [7] "DEVICEIDLETIME 9" resolution=4
  [8] "DEVICEIDLETIME 8" resolution=4
  [9] "DEVICEIDLETIME 7" resolution=4
  [10] "DEVICEIDLETIME 6" resolution=4
  [11] "DEVICEIDLETIME 5" resolution=4
  [12] "DEVICEIDLETIME 4" resolution=4
  [13] "DEVICEIDLETIME 3" resolution=4
  [14] "DEVICEIDLETIME 2" resolution=4
  [15] "IDLETIME" resolution=4
  [16] "SERVERTIME" resolution=4
testing system counter frequencies...
DEVICEIDLETIME 12... 999.984032
DEVICEIDLETIME 11... 999.876263
DEVICEIDLETIME 16... 999.964073
DEVICEIDLETIME 15... 999.888236
DEVICEIDLETIME 14... 999.958086
DEVICEIDLETIME 13... 999.798444
DEVICEIDLETIME 10... 999.956090
DEVICEIDLETIME 9... 1000.061880
DEVICEIDLETIME 8... 999.904201
DEVICEIDLETIME 7... 1000.177676
DEVICEIDLETIME 6... 999.960081
DEVICEIDLETIME 5... 1000.011976
DEVICEIDLETIME 4... 999.956090
DEVICEIDLETIME 3... 999.646831
DEVICEIDLETIME 2... 999.956090
IDLETIME... 999.802434
SERVERTIME... 999.720637

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment