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 #include "spdk/util.h" 40 41 static int g_trace_fd = -1; 42 static char g_shm_name[64]; 43 44 struct spdk_trace_histories *g_trace_histories; 45 46 void 47 _spdk_trace_record(uint64_t tsc, uint16_t tpoint_id, uint16_t poller_id, uint32_t size, 48 uint64_t object_id, uint64_t arg1) 49 { 50 struct spdk_trace_history *lcore_history; 51 struct spdk_trace_entry *next_entry; 52 unsigned lcore; 53 54 lcore = spdk_env_get_current_core(); 55 if (lcore >= SPDK_TRACE_MAX_LCORE) { 56 return; 57 } 58 59 lcore_history = spdk_get_per_lcore_history(g_trace_histories, lcore); 60 if (tsc == 0) { 61 tsc = spdk_get_ticks(); 62 } 63 64 lcore_history->tpoint_count[tpoint_id]++; 65 66 next_entry = &lcore_history->entries[lcore_history->next_entry]; 67 next_entry->tsc = tsc; 68 next_entry->tpoint_id = tpoint_id; 69 next_entry->poller_id = poller_id; 70 next_entry->size = size; 71 next_entry->object_id = object_id; 72 next_entry->arg1 = arg1; 73 74 lcore_history->next_entry++; 75 if (lcore_history->next_entry == lcore_history->num_entries) { 76 lcore_history->next_entry = 0; 77 } 78 } 79 80 int 81 spdk_trace_init(const char *shm_name, uint64_t num_entries) 82 { 83 int i = 0; 84 int histories_size; 85 uint64_t lcore_offsets[SPDK_TRACE_MAX_LCORE + 1]; 86 87 lcore_offsets[0] = sizeof(struct spdk_trace_flags); 88 for (i = 1; i < (int)SPDK_COUNTOF(lcore_offsets); i++) { 89 lcore_offsets[i] = spdk_get_trace_history_size(num_entries) + lcore_offsets[i - 1]; 90 } 91 histories_size = sizeof(struct spdk_trace_flags) + lcore_offsets[SPDK_TRACE_MAX_LCORE]; 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, histories_size) != 0) { 103 fprintf(stderr, "could not truncate shm\n"); 104 goto trace_init_err; 105 } 106 107 g_trace_histories = mmap(NULL, histories_size, 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 /* TODO: On FreeBSD, mlock on shm_open'd memory doesn't seem to work. Docs say that kern.ipc.shm_use_phys=1 115 * should allow it, but forcing that doesn't seem to work either. So for now just skip mlock on FreeBSD 116 * altogether. 117 */ 118 #if defined(__linux__) 119 if (mlock(g_trace_histories, histories_size) != 0) { 120 fprintf(stderr, "Could not mlock shm for tracing - %s.\n", spdk_strerror(errno)); 121 if (errno == ENOMEM) { 122 fprintf(stderr, "Check /dev/shm for old tracing files that can be deleted.\n"); 123 } 124 goto trace_init_err; 125 } 126 #endif 127 128 memset(g_trace_histories, 0, histories_size); 129 130 g_trace_flags = &g_trace_histories->flags; 131 132 g_trace_flags->tsc_rate = spdk_get_ticks_hz(); 133 134 for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) { 135 struct spdk_trace_history *lcore_history; 136 137 g_trace_flags->lcore_history_offsets[i] = lcore_offsets[i]; 138 lcore_history = spdk_get_per_lcore_history(g_trace_histories, i); 139 lcore_history->lcore = i; 140 lcore_history->num_entries = num_entries; 141 } 142 g_trace_flags->lcore_history_offsets[SPDK_TRACE_MAX_LCORE] = lcore_offsets[SPDK_TRACE_MAX_LCORE]; 143 144 spdk_trace_flags_init(); 145 146 return 0; 147 148 trace_init_err: 149 if (g_trace_histories != MAP_FAILED) { 150 munmap(g_trace_histories, histories_size); 151 } 152 close(g_trace_fd); 153 g_trace_fd = -1; 154 shm_unlink(shm_name); 155 g_trace_histories = NULL; 156 157 return 1; 158 159 } 160 161 void 162 spdk_trace_cleanup(void) 163 { 164 bool unlink; 165 int i; 166 struct spdk_trace_history *lcore_history; 167 168 if (g_trace_histories == NULL) { 169 return; 170 } 171 172 /* 173 * Only unlink the shm if there were no trace_entry recorded. This ensures the file 174 * can be used after this process exits/crashes for debugging. 175 * Note that we have to calculate this value before g_trace_histories gets unmapped. 176 */ 177 for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) { 178 lcore_history = spdk_get_per_lcore_history(g_trace_histories, i); 179 unlink = lcore_history->entries[0].tsc == 0; 180 if (!unlink) { 181 break; 182 } 183 } 184 185 munmap(g_trace_histories, sizeof(struct spdk_trace_histories)); 186 g_trace_histories = NULL; 187 close(g_trace_fd); 188 189 if (unlink) { 190 shm_unlink(g_shm_name); 191 } 192 } 193