cvmx-profiler.c revision 232809
1/***********************license start*************** 2 * Copyright (c) 2011 Cavium Inc. (support@cavium.com). All rights 3 * reserved. 4 * 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * * Redistributions in binary form must reproduce the above 14 * copyright notice, this list of conditions and the following 15 * disclaimer in the documentation and/or other materials provided 16 * with the distribution. 17 * 18 * * Neither the name of Cavium Inc. nor the names of 19 * its contributors may be used to endorse or promote products 20 * derived from this software without specific prior written 21 * permission. 22 * 23 * This Software, including technical data, may be subject to U.S. export control 24 * laws, including the U.S. Export Administration Act and its associated 25 * regulations, and may be subject to export or import regulations in other 26 * countries. 27 * 28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" 29 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR 30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO 31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR 32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM 33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE, 34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF 35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR 36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR 37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU. 38 ************************license end**************************************/ 39 40/** 41 * @file 42 * 43 * Interface to event profiler. 44 * 45 */ 46 47#include "cvmx-config.h" 48#include "cvmx.h" 49#include "cvmx-interrupt.h" 50#include "cvmx-sysinfo.h" 51#include "cvmx-coremask.h" 52#include "cvmx-spinlock.h" 53#include "cvmx-atomic.h" 54#include "cvmx-error.h" 55#include "cvmx-asm.h" 56#include "cvmx-bootmem.h" 57#include "cvmx-profiler.h" 58 59#ifdef PROFILER_DEBUG 60#define PRINTF(fmt, args...) cvmx_safe_printf(fmt, ##args) 61#else 62#define PRINTF(fmt, args...) 63#endif 64 65CVMX_SHARED static event_counter_control_block_t eccb; 66cvmx_config_block_t *pcpu_cfg_blk; 67 68int read_percpu_block = 1; 69 70/** 71 * Set Interrupt IRQ line for Performance Counter 72 * 73 */ 74void cvmx_update_perfcnt_irq(void) 75{ 76 uint64_t cvmctl; 77 78 /* Clear CvmCtl[IPPCI] bit and move the Performance Counter 79 * interrupt to IRQ 6 80 */ 81 CVMX_MF_COP0(cvmctl, COP0_CVMCTL); 82 cvmctl &= ~(7 << 7); 83 cvmctl |= 6 << 7; 84 CVMX_MT_COP0(cvmctl, COP0_CVMCTL); 85} 86 87/** 88 * @INTERNAL 89 * Return the baseaddress of the namedblock 90 * @param buf_name Name of Namedblock 91 * 92 * @return baseaddress of block on Success, NULL on failure. 93 */ 94static 95void *cvmx_get_memory_addr(const char* buf_name) 96{ 97 void *buffer_ptr = NULL; 98 const struct cvmx_bootmem_named_block_desc *block_desc = cvmx_bootmem_find_named_block(buf_name); 99 if (block_desc) 100 buffer_ptr = cvmx_phys_to_ptr(block_desc->base_addr); 101 assert (buffer_ptr != NULL); 102 103 return buffer_ptr; 104} 105 106/** 107 * @INTERNAL 108 * Initialize the cpu block metadata. 109 * 110 * @param cpu core no 111 * @param size size of per cpu memory in named block 112 * 113 */ 114static 115void cvmx_init_pcpu_block(int cpu, int size) 116{ 117 eccb.cfg_blk.pcpu_base_addr[cpu] = (char *)cvmx_get_memory_addr(EVENT_BUFFER_BLOCK) + (size * cpu); 118 assert (eccb.cfg_blk.pcpu_base_addr[cpu] != NULL); 119 120 cvmx_ringbuf_t *cpu_buf = (cvmx_ringbuf_t *) eccb.cfg_blk.pcpu_base_addr[cpu]; 121 122 cpu_buf->pcpu_blk_info.size = size; 123 cpu_buf->pcpu_blk_info.max_samples = ((size - sizeof(cvmx_cpu_event_block_t)) / sizeof(cvmx_sample_entry_t)); 124 cpu_buf->pcpu_blk_info.sample_count = 0; 125 cpu_buf->pcpu_blk_info.sample_read = 0; 126 cpu_buf->pcpu_blk_info.data = eccb.cfg_blk.pcpu_base_addr[cpu] + sizeof(cvmx_cpu_event_block_t) + PADBYTES; 127 cpu_buf->pcpu_blk_info.head = cpu_buf->pcpu_blk_info.tail = \ 128 cpu_buf->pcpu_data = cpu_buf->pcpu_blk_info.data; 129 cpu_buf->pcpu_blk_info.end = eccb.cfg_blk.pcpu_base_addr[cpu] + size; 130 131 cvmx_atomic_set32(&read_percpu_block, 0); 132 133 /* 134 * Write per cpu mem base address info in to 'event config' named block, 135 * This info is needed by oct-remote-profile to get Per cpu memory 136 * base address of each core of the named block. 137 */ 138 pcpu_cfg_blk = (cvmx_config_block_t *) eccb.config_blk_base_addr; 139 pcpu_cfg_blk->pcpu_base_addr[cpu] = eccb.cfg_blk.pcpu_base_addr[cpu]; 140} 141 142/** 143 * @INTERNAL 144 * Retrieve the info from the 'event_config' named block. 145 * 146 * Here events value is read(as passed to oct-remote-profile) to reset perf 147 * counters on every Perf counter overflow. 148 * 149 */ 150static 151void cvmx_read_config_blk(void) 152{ 153 eccb.config_blk_base_addr = (char *)cvmx_get_memory_addr(EVENT_BUFFER_CONFIG_BLOCK); 154 memcpy(&(eccb.cfg_blk.events), eccb.config_blk_base_addr + \ 155 offsetof(cvmx_config_block_t, events), sizeof(int64_t)); 156 157 cvmx_atomic_set32(&eccb.read_cfg_blk,1); 158 PRINTF("cfg_blk.events=%lu, sample_count=%ld\n", eccb.cfg_blk.events, eccb.cfg_blk.sample_count); 159} 160 161/** 162 * @INTERNAL 163 * Add new sample to the buffer and increment the head pointer and 164 * global sample count(i.e sum total of samples collected on all cores) 165 * 166 */ 167static 168void cvmx_add_sample_to_buffer(void) 169{ 170 uint32_t epc; 171 int cpu = cvmx_get_core_num(); 172 CVMX_MF_COP0(epc, COP0_EPC); 173 174 cvmx_ringbuf_t *cpu_buf = (cvmx_ringbuf_t *) eccb.cfg_blk.pcpu_base_addr[cpu]; 175 176 /* 177 * head/tail pointer can be NULL, and this case arises when oct-remote-profile is 178 * invoked afresh. To keep memory sane for current instance, we clear namedblock off 179 * previous data and this is accomplished by octeon_remote_write_mem from host. 180 */ 181 if (cvmx_unlikely(!cpu_buf->pcpu_blk_info.head && !cpu_buf->pcpu_blk_info.end)) { 182 /* Reread the event count as a different threshold val could be 183 * passed with profiler alongside --events flag */ 184 cvmx_read_config_blk(); 185 cvmx_init_pcpu_block(cpu, EVENT_PERCPU_BUFFER_SIZE); 186 } 187 188 /* In case of hitting end of buffer, reset head,data ptr to start */ 189 if (cpu_buf->pcpu_blk_info.head == cpu_buf->pcpu_blk_info.end) 190 cpu_buf->pcpu_blk_info.head = cpu_buf->pcpu_blk_info.data = cpu_buf->pcpu_data; 191 192 /* Store the pc, respective core no.*/ 193 cvmx_sample_entry_t *sample = (cvmx_sample_entry_t *) cpu_buf->pcpu_blk_info.data; 194 sample->pc = epc; 195 sample->core = cpu; 196 197 /* Update Per CPU stats */ 198 cpu_buf->pcpu_blk_info.sample_count++; 199 cpu_buf->pcpu_blk_info.data += sizeof(cvmx_sample_entry_t); 200 cpu_buf->pcpu_blk_info.head = cpu_buf->pcpu_blk_info.data; 201 202 /* Increment the global sample count i.e sum total of samples on all cores*/ 203 cvmx_atomic_add64(&(pcpu_cfg_blk->sample_count), 1); 204 205 PRINTF("the core%d:pc 0x%016lx, sample_count=%ld\n", cpu, sample->pc, cpu_buf->pcpu_blk_info.sample_count); 206} 207 208/** 209 * @INTERNAL 210 * Reset performance counters 211 * 212 * @param pf The performance counter Number (0, 1) 213 * @param events The threshold value for which interrupt has to be asserted 214 */ 215static 216void cvmx_reset_perf_counter(int pf, uint64_t events) 217{ 218 uint64_t pfc; 219 pfc = (1ull << 63) - events; 220 221 if (!pf) { 222 CVMX_MT_COP0(pfc, COP0_PERFVALUE0); 223 } else 224 CVMX_MT_COP0(pfc, COP0_PERFVALUE1); 225} 226 227void cvmx_collect_sample(void) 228{ 229 if (!eccb.read_cfg_blk) 230 cvmx_read_config_blk(); 231 232 if (read_percpu_block) 233 cvmx_init_pcpu_block(cvmx_get_core_num(), EVENT_PERCPU_BUFFER_SIZE); 234 235 cvmx_add_sample_to_buffer(); 236 cvmx_reset_perf_counter(0, eccb.cfg_blk.events); 237} 238