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 struct spdk_trace_histories *g_trace_histories; 44 45 void 46 _spdk_trace_record(uint64_t tsc, 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 unsigned lcore; 52 53 lcore = spdk_env_get_current_core(); 54 if (lcore >= SPDK_TRACE_MAX_LCORE) { 55 return; 56 } 57 58 lcore_history = &g_trace_histories->per_lcore_history[lcore]; 59 if (tsc == 0) { 60 tsc = spdk_get_ticks(); 61 } 62 63 lcore_history->tpoint_count[tpoint_id]++; 64 65 next_entry = &lcore_history->entries[lcore_history->next_entry]; 66 next_entry->tsc = tsc; 67 next_entry->tpoint_id = tpoint_id; 68 next_entry->poller_id = poller_id; 69 next_entry->size = size; 70 next_entry->object_id = object_id; 71 next_entry->arg1 = arg1; 72 73 lcore_history->next_entry++; 74 if (lcore_history->next_entry == SPDK_TRACE_SIZE) { 75 lcore_history->next_entry = 0; 76 } 77 } 78 79 int 80 spdk_trace_init(const char *shm_name) 81 { 82 int i = 0; 83 84 snprintf(g_shm_name, sizeof(g_shm_name), "%s", shm_name); 85 86 g_trace_fd = shm_open(shm_name, O_RDWR | O_CREAT, 0600); 87 if (g_trace_fd == -1) { 88 fprintf(stderr, "could not shm_open spdk_trace\n"); 89 fprintf(stderr, "errno=%d %s\n", errno, spdk_strerror(errno)); 90 return 1; 91 } 92 93 if (ftruncate(g_trace_fd, sizeof(*g_trace_histories)) != 0) { 94 fprintf(stderr, "could not truncate shm\n"); 95 goto trace_init_err; 96 } 97 98 g_trace_histories = mmap(NULL, sizeof(*g_trace_histories), PROT_READ | PROT_WRITE, 99 MAP_SHARED, g_trace_fd, 0); 100 if (g_trace_histories == MAP_FAILED) { 101 fprintf(stderr, "could not mmap shm\n"); 102 goto trace_init_err; 103 } 104 105 /* TODO: On FreeBSD, mlock on shm_open'd memory doesn't seem to work. Docs say that kern.ipc.shm_use_phys=1 106 * should allow it, but forcing that doesn't seem to work either. So for now just skip mlock on FreeBSD 107 * altogether. 108 */ 109 #if defined(__linux__) 110 if (mlock(g_trace_histories, sizeof(*g_trace_histories)) != 0) { 111 fprintf(stderr, "Could not mlock shm for tracing - %s.\n", spdk_strerror(errno)); 112 if (errno == ENOMEM) { 113 fprintf(stderr, "Check /dev/shm for old tracing files that can be deleted.\n"); 114 } 115 goto trace_init_err; 116 } 117 #endif 118 119 memset(g_trace_histories, 0, sizeof(*g_trace_histories)); 120 121 g_trace_flags = &g_trace_histories->flags; 122 123 g_trace_flags->tsc_rate = spdk_get_ticks_hz(); 124 125 for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) { 126 g_trace_histories->per_lcore_history[i].lcore = i; 127 } 128 129 spdk_trace_flags_init(); 130 131 return 0; 132 133 trace_init_err: 134 if (g_trace_histories != MAP_FAILED) { 135 munmap(g_trace_histories, sizeof(*g_trace_histories)); 136 } 137 close(g_trace_fd); 138 g_trace_fd = -1; 139 shm_unlink(shm_name); 140 g_trace_histories = NULL; 141 142 return 1; 143 144 } 145 146 void 147 spdk_trace_cleanup(void) 148 { 149 bool unlink; 150 151 if (g_trace_histories == NULL) { 152 return; 153 } 154 155 /* 156 * Only unlink the shm if there were no tracepoints enabled. This ensures the file 157 * can be used after this process exits/crashes for debugging. 158 * Note that we have to calculate this value before g_trace_histories gets unmapped. 159 */ 160 unlink = spdk_mem_all_zero(g_trace_flags->tpoint_mask, sizeof(g_trace_flags->tpoint_mask)); 161 munmap(g_trace_histories, sizeof(struct spdk_trace_histories)); 162 g_trace_histories = NULL; 163 close(g_trace_fd); 164 165 if (unlink) { 166 shm_unlink(g_shm_name); 167 } 168 } 169