linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* How to measure VM events using perf-event
@ 2015-04-10  3:46 Jueyuan Zhu
  2015-04-10 15:39 ` Christopher Covington
  0 siblings, 1 reply; 6+ messages in thread
From: Jueyuan Zhu @ 2015-04-10  3:46 UTC (permalink / raw)
  To: linux-perf-users

Hello,

I am trying to use the perf-event to measure the events for one specified VM in KVM platform. Since each VM is just a process in the host OS, so I thought we could just set the process id of this VM in the perf-event_open API to count the events. The following is my code (based on the example from perf_event_open linux manual). 

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/perf_event.h>
#include <asm/unistd.h>

int main(int argc, char **argv)
{
    struct perf_event_attr pe;
    long long count;
    int fd;
    int pid = VM_id; // the process id of the VM.

    memset(&pe, 0, sizeof(struct perf_event_attr));
    pe.size = sizeof(struct perf_event_attr);
    pe.type = PERF_TYPE_HARDWARE;
    pe.config = PERF_COUNT_HW_INSTRUCTIONS;
    pe.disabled = 1;
    pe.exclude_kernel = 0;
    pe.exclude_hv = 0;
    pe.exclude_host = 0;
    pe.exclude_guest = 0;

    fd = syscall(__NR_perf_event_open, &pe, pid, -1, -1, 0);
    if (fd == -1) {
       fprintf(stderr, "Error opening leader %llx\n", pe.config);
       exit(EXIT_FAILURE);
    }

    ioctl(fd, PERF_EVENT_IOC_RESET, 0);
    ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);

    sleep(1);

    ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
    read(fd, &count, sizeof(long long));

    printf("# of instructions:  %lld\n", count);

    close(fd);
}

However, this program’s output keeps constant whenever this VM is idle, or running different benchmarks. So it seems the event counting is incorrect. Can anyone tell me how to set the flags or perf_event_attr to correctly count the VM events using perf_event_open? 

Thank you very much in advance!!

Best regards,

Jueyuan--
To unsubscribe from this list: send the line "unsubscribe linux-perf-users" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-04-11 11:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-04-10  3:46 How to measure VM events using perf-event Jueyuan Zhu
2015-04-10 15:39 ` Christopher Covington
2015-04-10 16:17   ` Jueyuan Zhu
2015-04-10 16:57     ` Christopher Covington
2015-04-11  2:45     ` Tianwei Zhang
2015-04-11 11:59     ` Arnaldo Carvalho de Melo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).