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
11arg1 = sys.argv.pop(1)
12arg2 = ""
13if len(sys.argv) > 1:
14  arg2 = sys.argv.pop(1)
15
16
17class TestBlkRequest(TestCase):
18    def setUp(self):
19        b = BPF(arg1, arg2, debug=0)
20        self.latency = b.get_table("latency", c_uint, c_ulong)
21        b.attach_kprobe(event="blk_start_request",
22                fn_name="probe_blk_start_request")
23        b.attach_kprobe(event="blk_update_request",
24                fn_name="probe_blk_update_request")
25
26    def test_blk1(self):
27        import subprocess
28        import os
29        # use /opt instead of /tmp so that it hits a real disk
30        for i in range(0, 2):
31            subprocess.call(["dd", "if=/dev/zero", "of=/opt/trace3.txt",
32                             "count=1024", "bs=4096"])
33            subprocess.call(["sync"])
34        os.unlink("/opt/trace3.txt")
35        for key, leaf in self.latency.items():
36            print("latency %u:" % key.value, "count %u" % leaf.value)
37        sys.stdout.flush()
38        self.assertEqual(len(list(self.latency.keys())), len(self.latency))
39
40if __name__ == "__main__":
41    main()
42