• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright (c) PLUMgrid, Inc.
3# Licensed under the Apache License, Version 2.0 (the "License")
4
5from ctypes import c_uint, c_ulong, Structure
6from bcc import BPF
7from time import sleep
8import sys
9from unittest import main, TestCase
10
11text = """
12#include <linux/ptrace.h>
13struct Ptr { u64 ptr; };
14struct Counters { char unused; __int128 stat1; };
15BPF_HASH(stats, struct Ptr, struct Counters, 1024);
16
17int count_sched(struct pt_regs *ctx) {
18  struct Ptr key = {.ptr=PT_REGS_PARM1(ctx)};
19  struct Counters zleaf;
20
21  memset(&zleaf, 0, sizeof(zleaf));
22  stats.lookup_or_init(&key, &zleaf)->stat1++;
23  return 0;
24}
25"""
26
27class TestTracingEvent(TestCase):
28    def setUp(self):
29        b = BPF(text=text, debug=0)
30        self.stats = b.get_table("stats")
31        b.attach_kprobe(event="finish_task_switch", fn_name="count_sched")
32
33    def test_sched1(self):
34        for i in range(0, 100):
35            sleep(0.01)
36        for key, leaf in self.stats.items():
37            print("ptr %x:" % key.ptr, "stat1 (%d %d)" % (leaf.stat1[1], leaf.stat1[0]))
38
39if __name__ == "__main__":
40    main()
41