+ int ncpus;
+ int totalspace;
+ int nevents_per_cpu;
+ lwt_event_t *events;
+ lwt_event_t *cpu_event[LWT_MAX_CPUS + 1];
+ lwt_event_t *next_event[LWT_MAX_CPUS];
+ lwt_event_t *first_event[LWT_MAX_CPUS];
+ int cpu;
+ lwt_event_t *e;
+ int rc;
+ int i;
+ double mhz;
+ cycles_t t0;
+ cycles_t tlast;
+ cycles_t tnow;
+ struct timeval tvnow;
+ int printed_date = 0;
+ int nlines = 0;
+ FILE *f = stdout;
+
+ if (argc < 2 ||
+ (strcmp(argv[1], "start") &&
+ strcmp(argv[1], "stop"))) {
+ fprintf(stderr,
+ "usage: %s start\n"
+ " %s stop [fname]\n", argv[0], argv[0]);
+ return (-1);
+ }
+
+ if (!strcmp(argv[1], "start")) {
+ /* disable */
+ if (lwt_control(0, 0) != 0)
+ return (-1);
+
+ /* clear */
+ if (lwt_control(0, 1) != 0)
+ return (-1);
+
+ /* enable */
+ if (lwt_control(1, 0) != 0)
+ return (-1);
+
+ return (0);
+ }
+
+ if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
+ return (-1);
+
+ if (ncpus > LWT_MAX_CPUS) {
+ fprintf(stderr, "Too many cpus: %d (%d)\n",
+ ncpus, LWT_MAX_CPUS);
+ return (-1);
+ }
+
+ events = (lwt_event_t *)malloc(totalspace);
+ if (events == NULL) {
+ fprintf(stderr, "Can't allocate %d\n", totalspace);
+ return (-1);
+ }
+
+ if (lwt_control(0, 0) != 0) { /* disable */
+ free(events);
+ return (-1);
+ }
+
+ if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
+ free(events);
+ return (-1);
+ }
+
+ /* we want this time to be sampled at snapshot time */
+ gettimeofday(&tvnow, NULL);
+
+ if (argc > 2) {
+ f = fopen (argv[2], "w");
+ if (f == NULL) {
+ fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
+ free(events);
+ return (-1);
+ }
+ }
+
+ mhz = get_cycles_per_usec();
+
+ /* carve events into per-cpu slices */
+ nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
+ for (cpu = 0; cpu <= ncpus; cpu++)
+ cpu_event[cpu] = &events[cpu * nevents_per_cpu];
+
+ /* find the earliest event on each cpu */
+ for (cpu = 0; cpu < ncpus; cpu++) {
+ first_event[cpu] = NULL;
+
+ for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
+
+ if (e->lwte_where == NULL) /* not an event */
+ continue;
+
+ if (first_event[cpu] == NULL ||
+ first_event[cpu]->lwte_when > e->lwte_when)
+ first_event[cpu] = e;
+ }
+
+ next_event[cpu] = first_event[cpu];
+ }
+
+ t0 = tlast = 0;
+ for (cpu = 0; cpu < ncpus; cpu++) {
+ e = first_event[cpu];
+ if (e == NULL) /* no events this cpu */
+ continue;
+
+ if (e == cpu_event[cpu])
+ e = cpu_event[cpu + 1] - 1;
+ else
+ e = e - 1;
+
+ /* If there's an event immediately before the first one, this
+ * cpu wrapped its event buffer */
+ if (e->lwte_where == NULL)
+ continue;
+
+ /* We should only start outputting events from the most recent
+ * first event in any wrapped cpu. Events before this time on
+ * other cpus won't have any events from this CPU to interleave
+ * with. */
+ if (t0 < first_event[cpu]->lwte_when)
+ t0 = first_event[cpu]->lwte_when;
+ }
+
+ for (;;) {
+ /* find which cpu has the next event */
+ cpu = -1;
+ for (i = 0; i < ncpus; i++) {
+
+ if (next_event[i] == NULL) /* this cpu exhausted */
+ continue;
+
+ if (cpu < 0 ||
+ next_event[i]->lwte_when < next_event[cpu]->lwte_when)
+ cpu = i;
+ }
+
+ if (cpu < 0) /* all cpus exhausted */