Created
November 3, 2019 22:53
-
-
Save JC3/fe3780cdfe609009e43ca14192a277eb to your computer and use it in GitHub Desktop.
x sync extension system counter list and test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output on my machine: