2 * Copyright (C) ARM Limited 2010-2014. All rights reserved.
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.
10 static void marshal_frame(int cpu, int buftype)
14 if (!per_cpu(gator_buffer, cpu)[buftype]) {
20 frame = FRAME_SUMMARY;
23 frame = FRAME_BACKTRACE;
29 frame = FRAME_COUNTER;
31 case BLOCK_COUNTER_BUF:
32 frame = FRAME_BLOCK_COUNTER;
35 frame = FRAME_ANNOTATE;
38 frame = FRAME_SCHED_TRACE;
44 frame = FRAME_ACTIVITY;
52 if (gator_response_type > 0) {
53 gator_buffer_write_packed_int(cpu, buftype, gator_response_type);
56 // leave space for 4-byte unpacked length
57 per_cpu(gator_buffer_write, cpu)[buftype] = (per_cpu(gator_buffer_write, cpu)[buftype] + sizeof(s32)) & gator_buffer_mask[buftype];
59 // add frame type and core number
60 gator_buffer_write_packed_int(cpu, buftype, frame);
61 gator_buffer_write_packed_int(cpu, buftype, cpu);
64 static int buffer_bytes_available(int cpu, int buftype)
66 int remaining, filled;
68 filled = per_cpu(gator_buffer_write, cpu)[buftype] - per_cpu(gator_buffer_read, cpu)[buftype];
70 filled += gator_buffer_size[buftype];
73 remaining = gator_buffer_size[buftype] - filled;
75 if (per_cpu(buffer_space_available, cpu)[buftype]) {
76 // Give some extra room; also allows space to insert the overflow error packet
79 // Hysteresis, prevents multiple overflow messages
86 static bool buffer_check_space(int cpu, int buftype, int bytes)
88 int remaining = buffer_bytes_available(cpu, buftype);
90 if (remaining < bytes) {
91 per_cpu(buffer_space_available, cpu)[buftype] = false;
93 per_cpu(buffer_space_available, cpu)[buftype] = true;
96 return per_cpu(buffer_space_available, cpu)[buftype];
99 static int contiguous_space_available(int cpu, int buftype)
101 int remaining = buffer_bytes_available(cpu, buftype);
102 int contiguous = gator_buffer_size[buftype] - per_cpu(gator_buffer_write, cpu)[buftype];
103 if (remaining < contiguous)
109 static void gator_commit_buffer(int cpu, int buftype, u64 time)
111 int type_length, commit, length, byte;
114 if (!per_cpu(gator_buffer, cpu)[buftype])
117 // post-populate the length, which does not include the response type length nor the length itself, i.e. only the length of the payload
118 local_irq_save(flags);
119 type_length = gator_response_type ? 1 : 0;
120 commit = per_cpu(gator_buffer_commit, cpu)[buftype];
121 length = per_cpu(gator_buffer_write, cpu)[buftype] - commit;
123 length += gator_buffer_size[buftype];
125 length = length - type_length - sizeof(s32);
127 if (length <= FRAME_HEADER_SIZE) {
128 // Nothing to write, only the frame header is present
129 local_irq_restore(flags);
133 for (byte = 0; byte < sizeof(s32); byte++) {
134 per_cpu(gator_buffer, cpu)[buftype][(commit + type_length + byte) & gator_buffer_mask[buftype]] = (length >> byte * 8) & 0xFF;
137 per_cpu(gator_buffer_commit, cpu)[buftype] = per_cpu(gator_buffer_write, cpu)[buftype];
139 if (gator_live_rate > 0) {
140 while (time > per_cpu(gator_buffer_commit_time, cpu)) {
141 per_cpu(gator_buffer_commit_time, cpu) += gator_live_rate;
145 marshal_frame(cpu, buftype);
146 local_irq_restore(flags);
148 // had to delay scheduling work as attempting to schedule work during the context switch is illegal in kernel versions 3.5 and greater
149 if (per_cpu(in_scheduler_context, cpu)) {
150 #ifndef CONFIG_PREEMPT_RT_FULL
151 // mod_timer can not be used in interrupt context in RT-Preempt full
152 mod_timer(&gator_buffer_wake_up_timer, jiffies + 1);
155 up(&gator_buffer_wake_sem);
159 static void buffer_check(int cpu, int buftype, u64 time)
161 int filled = per_cpu(gator_buffer_write, cpu)[buftype] - per_cpu(gator_buffer_commit, cpu)[buftype];
163 filled += gator_buffer_size[buftype];
165 if (filled >= ((gator_buffer_size[buftype] * 3) / 4)) {
166 gator_commit_buffer(cpu, buftype, time);