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 int g_trace_fd = -1; 41 static char g_shm_name[64]; 42 43 static struct spdk_trace_histories *g_trace_histories; 44 45 void 46 spdk_trace_record(uint16_t tpoint_id, uint16_t poller_id, uint32_t size, 47 uint64_t object_id, uint64_t arg1) 48 { 49 struct spdk_trace_history *lcore_history; 50 struct spdk_trace_entry *next_entry; 51 uint64_t tsc; 52 unsigned lcore; 53 54 /* 55 * Tracepoint group ID is encoded in the tpoint_id. Lower 6 bits determine the tracepoint 56 * within the group, the remaining upper bits determine the tracepoint group. Each 57 * tracepoint group has its own tracepoint mask. 58 */ 59 if (g_trace_histories == NULL || 60 !((1ULL << (tpoint_id & 0x3F)) & g_trace_histories->flags.tpoint_mask[tpoint_id >> 6])) { 61 return; 62 } 63 64 lcore = spdk_env_get_current_core(); 65 if (lcore >= SPDK_TRACE_MAX_LCORE) { 66 return; 67 } 68 69 lcore_history = &g_trace_histories->per_lcore_history[lcore]; 70 tsc = spdk_get_ticks(); 71 72 lcore_history->tpoint_count[tpoint_id]++; 73 74 next_entry = &lcore_history->entries[lcore_history->next_entry]; 75 next_entry->tsc = tsc; 76 next_entry->tpoint_id = tpoint_id; 77 next_entry->poller_id = poller_id; 78 next_entry->size = size; 79 next_entry->object_id = object_id; 80 next_entry->arg1 = arg1; 81 82 lcore_history->next_entry++; 83 if (lcore_history->next_entry == SPDK_TRACE_SIZE) { 84 lcore_history->next_entry = 0; 85 } 86 } 87 88 int 89 spdk_trace_init(const char *shm_name) 90 { 91 int i = 0; 92 93 snprintf(g_shm_name, sizeof(g_shm_name), "%s", shm_name); 94 95 g_trace_fd = shm_open(shm_name, O_RDWR | O_CREAT, 0600); 96 if (g_trace_fd == -1) { 97 fprintf(stderr, "could not shm_open spdk_trace\n"); 98 fprintf(stderr, "errno=%d %s\n", errno, spdk_strerror(errno)); 99 return 1; 100 } 101 102 if (ftruncate(g_trace_fd, sizeof(*g_trace_histories)) != 0) { 103 fprintf(stderr, "could not truncate shm\n"); 104 goto trace_init_err; 105 } 106 107 g_trace_histories = mmap(NULL, sizeof(*g_trace_histories), PROT_READ | PROT_WRITE, 108 MAP_SHARED, g_trace_fd, 0); 109 if (g_trace_histories == MAP_FAILED) { 110 fprintf(stderr, "could not mmap shm\n"); 111 goto trace_init_err; 112 } 113 114 memset(g_trace_histories, 0, sizeof(*g_trace_histories)); 115 116 g_trace_flags = &g_trace_histories->flags; 117 118 g_trace_flags->tsc_rate = spdk_get_ticks_hz(); 119 120 for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) { 121 g_trace_histories->per_lcore_history[i].lcore = i; 122 } 123 124 spdk_trace_flags_init(); 125 126 return 0; 127 128 trace_init_err: 129 close(g_trace_fd); 130 g_trace_fd = -1; 131 shm_unlink(shm_name); 132 133 return 1; 134 135 } 136 137 void 138 spdk_trace_cleanup(void) 139 { 140 if (g_trace_histories) { 141 munmap(g_trace_histories, sizeof(struct spdk_trace_histories)); 142 g_trace_histories = NULL; 143 close(g_trace_fd); 144 } 145 shm_unlink(g_shm_name); 146 } 147