1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/stdinc.h" 35 36 #include "spdk/env.h" 37 #include "spdk/string.h" 38 #include "spdk/trace.h" 39 40 static char g_shm_name[64]; 41 42 static struct spdk_trace_histories *g_trace_histories; 43 44 void 45 spdk_trace_record(uint16_t tpoint_id, uint16_t poller_id, uint32_t size, 46 uint64_t object_id, uint64_t arg1) 47 { 48 struct spdk_trace_history *lcore_history; 49 struct spdk_trace_entry *next_entry; 50 uint64_t tsc; 51 unsigned lcore; 52 53 /* 54 * Tracepoint group ID is encoded in the tpoint_id. Lower 6 bits determine the tracepoint 55 * within the group, the remaining upper bits determine the tracepoint group. Each 56 * tracepoint group has its own tracepoint mask. 57 */ 58 if (g_trace_histories == NULL || 59 !((1ULL << (tpoint_id & 0x3F)) & g_trace_histories->flags.tpoint_mask[tpoint_id >> 6])) { 60 return; 61 } 62 63 lcore = spdk_env_get_current_core(); 64 if (lcore >= SPDK_TRACE_MAX_LCORE) { 65 return; 66 } 67 68 lcore_history = &g_trace_histories->per_lcore_history[lcore]; 69 tsc = spdk_get_ticks(); 70 71 lcore_history->tpoint_count[tpoint_id]++; 72 73 next_entry = &lcore_history->entries[lcore_history->next_entry]; 74 next_entry->tsc = tsc; 75 next_entry->tpoint_id = tpoint_id; 76 next_entry->poller_id = poller_id; 77 next_entry->size = size; 78 next_entry->object_id = object_id; 79 next_entry->arg1 = arg1; 80 81 lcore_history->next_entry++; 82 if (lcore_history->next_entry == SPDK_TRACE_SIZE) { 83 lcore_history->next_entry = 0; 84 } 85 } 86 87 void 88 spdk_trace_init(const char *shm_name) 89 { 90 int trace_fd; 91 int i = 0; 92 char buf[64]; 93 94 snprintf(g_shm_name, sizeof(g_shm_name), "%s", shm_name); 95 96 trace_fd = shm_open(shm_name, O_RDWR | O_CREAT, 0600); 97 if (trace_fd == -1) { 98 spdk_strerror_r(errno, buf, sizeof(buf)); 99 fprintf(stderr, "could not shm_open spdk_trace\n"); 100 fprintf(stderr, "errno=%d %s\n", errno, buf); 101 exit(EXIT_FAILURE); 102 } 103 104 if (ftruncate(trace_fd, sizeof(*g_trace_histories)) != 0) { 105 fprintf(stderr, "could not truncate shm\n"); 106 exit(EXIT_FAILURE); 107 } 108 109 g_trace_histories = mmap(NULL, sizeof(*g_trace_histories), PROT_READ | PROT_WRITE, 110 MAP_SHARED, trace_fd, 0); 111 if (g_trace_histories == NULL) { 112 fprintf(stderr, "could not mmap shm\n"); 113 exit(EXIT_FAILURE); 114 } 115 116 memset(g_trace_histories, 0, sizeof(*g_trace_histories)); 117 118 g_trace_flags = &g_trace_histories->flags; 119 120 g_trace_flags->tsc_rate = spdk_get_ticks_hz(); 121 122 for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) { 123 g_trace_histories->per_lcore_history[i].lcore = i; 124 } 125 126 spdk_trace_flags_init(); 127 } 128 129 void 130 spdk_trace_cleanup(void) 131 { 132 munmap(g_trace_histories, sizeof(struct spdk_trace_histories)); 133 shm_unlink(g_shm_name); 134 } 135