]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/arm-ds5-gator.git/blob - driver/gator_annotate.c
gator: Version 5.11
[android-sdk/arm-ds5-gator.git] / driver / gator_annotate.c
1 /**
2  * Copyright (C) ARM Limited 2010-2012. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  */
10 #include <linux/slab.h>
11 #include <linux/fs.h>
12 #include <linux/mm.h>
13 #include <linux/sched.h>
14 #include <asm/uaccess.h>
15 #include <asm/current.h>
16 #include <linux/spinlock.h>
18 static DEFINE_SPINLOCK(annotate_lock);
19 static bool collect_annotations = false;
21 static int annotate_copy(struct file *file, char const __user *buf, size_t count)
22 {
23         int cpu = 0;
24         int write = per_cpu(gator_buffer_write, cpu)[ANNOTATE_BUF];
26         if (file == NULL) {
27                 // copy from kernel
28                 memcpy(&per_cpu(gator_buffer, cpu)[ANNOTATE_BUF][write], buf, count);
29         } else {
30                 // copy from user space
31                 if (copy_from_user(&per_cpu(gator_buffer, cpu)[ANNOTATE_BUF][write], buf, count) != 0)
32                         return -1;
33         }
34         per_cpu(gator_buffer_write, cpu)[ANNOTATE_BUF] = (write + count) & gator_buffer_mask[ANNOTATE_BUF];
36         return 0;
37 }
39 static ssize_t annotate_write(struct file *file, char const __user *buf, size_t count_orig, loff_t *offset)
40 {
41         int tid, cpu, header_size, available, contiguous, length1, length2, size, count = count_orig & 0x7fffffff;
43         if (*offset)
44                 return -EINVAL;
46         if (!collect_annotations)
47                 // Not collecting annotations, tell the caller everything was written
48                 return count_orig;
50         cpu = 0; // Annotation only uses a single per-cpu buffer as the data must be in order to the engine
52         if (file == NULL) {
53                 tid = -1; // set the thread id to the kernel thread
54         } else {
55                 tid = current->pid;
56         }
58         // synchronize between cores
59         spin_lock(&annotate_lock);
61         // determine total size of the payload
62         header_size = MAXSIZE_PACK32 * 3 + MAXSIZE_PACK64;
63         available = buffer_bytes_available(cpu, ANNOTATE_BUF) - header_size;
64         size = count < available ? count : available;
66         if (size <= 0) {
67                 // Buffer is full but don't return an error.  Instead return 0 so the
68                 // caller knows nothing was written and they can try again.
69                 size = 0;
70                 goto annotate_write_out;
71         }
73         // synchronize shared variables annotateBuf and annotatePos
74         if (collect_annotations && per_cpu(gator_buffer, cpu)[ANNOTATE_BUF]) {
75                 gator_buffer_write_packed_int(cpu, ANNOTATE_BUF, smp_processor_id());
76                 gator_buffer_write_packed_int(cpu, ANNOTATE_BUF, tid);
77                 gator_buffer_write_packed_int64(cpu, ANNOTATE_BUF, gator_get_time());
78                 gator_buffer_write_packed_int(cpu, ANNOTATE_BUF, size);
80                 // determine the sizes to capture, length1 + length2 will equal size
81                 contiguous = contiguous_space_available(cpu, ANNOTATE_BUF);
82                 if (size < contiguous) {
83                         length1 = size;
84                         length2 = 0;
85                 } else {
86                         length1 = contiguous;
87                         length2 = size - contiguous;
88                 }
90                 if (annotate_copy(file, buf, length1) != 0) {
91                         size = -EINVAL;
92                         goto annotate_write_out;
93                 }
95                 if (length2 > 0 && annotate_copy(file, &buf[length1], length2) != 0) {
96                         size = -EINVAL;
97                         goto annotate_write_out;
98                 }
100                 // Check and commit; commit is set to occur once buffer is 3/4 full
101                 buffer_check(cpu, ANNOTATE_BUF);
102         }
104 annotate_write_out:
105         spin_unlock(&annotate_lock);
107         // return the number of bytes written
108         return size;
111 #include "gator_annotate_kernel.c"
113 static int annotate_release(struct inode *inode, struct file *file)
115         int cpu = 0;
117         // synchronize between cores
118         spin_lock(&annotate_lock);
120         if (per_cpu(gator_buffer, cpu)[ANNOTATE_BUF] && buffer_check_space(cpu, ANNOTATE_BUF, MAXSIZE_PACK64 + 3 * MAXSIZE_PACK32)) {
121                 uint32_t tid = current->pid;
122                 gator_buffer_write_packed_int(cpu, ANNOTATE_BUF, smp_processor_id());
123                 gator_buffer_write_packed_int(cpu, ANNOTATE_BUF, tid);
124                 gator_buffer_write_packed_int64(cpu, ANNOTATE_BUF, 0); // time
125                 gator_buffer_write_packed_int(cpu, ANNOTATE_BUF, 0);   // size
126         }
128         spin_unlock(&annotate_lock);
130         return 0;
133 static const struct file_operations annotate_fops = {
134         .write          = annotate_write,
135         .release        = annotate_release
136 };
138 static int gator_annotate_create_files(struct super_block *sb, struct dentry *root)
140         return gatorfs_create_file_perm(sb, root, "annotate", &annotate_fops, 0666);
143 static int gator_annotate_start(void)
145         collect_annotations = true;
146         return 0;
149 static void gator_annotate_stop(void)
151         collect_annotations = false;