2 * Example events provider
4 * Copyright (C) ARM Limited 2010-2015. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Similar entries to those below must be present in the events.xml file.
11 * To add them to the events.xml, create an events-mmap.xml with the
12 * following contents and rebuild gatord:
14 * <category name="mmapped">
15 * <event counter="mmapped_cnt0" title="Simulated1" name="Sine" display="maximum" class="absolute" description="Sort-of-sine"/>
16 * <event counter="mmapped_cnt1" title="Simulated2" name="Triangle" display="maximum" class="absolute" description="Triangular wave"/>
17 * <event counter="mmapped_cnt2" title="Simulated3" name="PWM" display="maximum" class="absolute" description="PWM Signal"/>
20 * When adding custom events, be sure to do the following:
21 * - add any needed .c files to the gator driver Makefile
22 * - call gator_events_install in the events init function
23 * - add the init function to GATOR_EVENTS_LIST in gator_main.c
24 * - add a new events-*.xml file to the gator daemon and rebuild
27 * - verify the new events are part of events.xml, which is created when building the daemon
28 * - verify the new events exist at /dev/gator/events/ once gatord is launched
29 * - verify the counter name in the XML matches the name at /dev/gator/events
32 #include <linux/init.h>
34 #include <linux/ratelimit.h>
38 #define MMAPPED_COUNTERS_NUM 3
40 static int mmapped_global_enabled;
43 unsigned long enabled;
45 } mmapped_counters[MMAPPED_COUNTERS_NUM];
47 static int mmapped_buffer[MMAPPED_COUNTERS_NUM * 2];
51 /* Adds mmapped_cntX directories and enabled, event, and key files to /dev/gator/events */
52 static int gator_events_mmapped_create_files(struct super_block *sb,
57 for (i = 0; i < MMAPPED_COUNTERS_NUM; i++) {
61 snprintf(buf, sizeof(buf), "mmapped_cnt%d", i);
62 dir = gatorfs_mkdir(sb, root, buf);
65 gatorfs_create_ulong(sb, dir, "enabled",
66 &mmapped_counters[i].enabled);
67 gatorfs_create_ro_ulong(sb, dir, "key",
68 &mmapped_counters[i].key);
74 static int gator_events_mmapped_start(void)
80 prev_time = timespec_to_ns(&ts);
82 mmapped_global_enabled = 0;
83 for (i = 0; i < MMAPPED_COUNTERS_NUM; i++) {
84 if (mmapped_counters[i].enabled) {
85 mmapped_global_enabled = 1;
93 static void gator_events_mmapped_stop(void)
97 /* This function "simulates" counters, generating values of fancy
98 * functions like sine or triangle... */
99 static int mmapped_simulate(int counter, int delta_in_us)
104 case 0: /* sort-of-sine */
113 if (t % 1024000 < 512000)
114 x = 512000 - (t % 512000);
118 result = 32 * x / 512000;
119 result = result * result;
122 result = 1922 - result;
125 case 1: /* triangle */
129 v = v + d * delta_in_us;
133 } else if (v > 1000000) {
141 case 2: /* PWM signal */
148 if (x / 1000000 != (x + delta_in_us) / 1000000)
149 dc = (dc + 100000) % 1000000;
152 result = t < dc ? 0 : 10;
160 static int gator_events_mmapped_read(int **buffer, bool sched_switch)
168 /* System wide counters - read from one core only */
169 if (!on_primary_core() || !mmapped_global_enabled)
173 time = timespec_to_ns(&ts);
174 delta_in_us = (int)(time - prev_time) / 1000;
177 for (i = 0; i < MMAPPED_COUNTERS_NUM; i++) {
178 if (mmapped_counters[i].enabled) {
179 mmapped_buffer[len++] = mmapped_counters[i].key;
180 mmapped_buffer[len++] =
181 mmapped_simulate(i, delta_in_us);
186 *buffer = mmapped_buffer;
191 static struct gator_interface gator_events_mmapped_interface = {
192 .create_files = gator_events_mmapped_create_files,
193 .start = gator_events_mmapped_start,
194 .stop = gator_events_mmapped_stop,
195 .read = gator_events_mmapped_read,
198 /* Must not be static! */
199 int __init gator_events_mmapped_init(void)
203 for (i = 0; i < MMAPPED_COUNTERS_NUM; i++) {
204 mmapped_counters[i].enabled = 0;
205 mmapped_counters[i].key = gator_events_get_key();
208 return gator_events_install(&gator_events_mmapped_interface);