]> git.proxmox.com Git - ceph.git/blame - ceph/src/spdk/lib/trace/trace.c
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / spdk / lib / trace / trace.c
CommitLineData
7c673cae
FG
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
11fdf7f2
TL
34#include "spdk/stdinc.h"
35
7c673cae 36#include "spdk/env.h"
11fdf7f2 37#include "spdk/string.h"
7c673cae 38#include "spdk/trace.h"
9f95a23c
TL
39#include "spdk/util.h"
40#include "spdk/barrier.h"
f67539c2 41#include "spdk/log.h"
7c673cae 42
11fdf7f2 43static int g_trace_fd = -1;
7c673cae
FG
44static char g_shm_name[64];
45
11fdf7f2 46struct spdk_trace_histories *g_trace_histories;
7c673cae
FG
47
48void
11fdf7f2
TL
49_spdk_trace_record(uint64_t tsc, uint16_t tpoint_id, uint16_t poller_id, uint32_t size,
50 uint64_t object_id, uint64_t arg1)
7c673cae
FG
51{
52 struct spdk_trace_history *lcore_history;
53 struct spdk_trace_entry *next_entry;
7c673cae 54 unsigned lcore;
9f95a23c 55 uint64_t next_circular_entry;
7c673cae 56
11fdf7f2 57 lcore = spdk_env_get_current_core();
7c673cae
FG
58 if (lcore >= SPDK_TRACE_MAX_LCORE) {
59 return;
60 }
61
9f95a23c 62 lcore_history = spdk_get_per_lcore_history(g_trace_histories, lcore);
11fdf7f2
TL
63 if (tsc == 0) {
64 tsc = spdk_get_ticks();
65 }
7c673cae
FG
66
67 lcore_history->tpoint_count[tpoint_id]++;
68
9f95a23c
TL
69 /* Get next entry index in the circular buffer */
70 next_circular_entry = lcore_history->next_entry & (lcore_history->num_entries - 1);
71 next_entry = &lcore_history->entries[next_circular_entry];
7c673cae
FG
72 next_entry->tsc = tsc;
73 next_entry->tpoint_id = tpoint_id;
74 next_entry->poller_id = poller_id;
75 next_entry->size = size;
76 next_entry->object_id = object_id;
77 next_entry->arg1 = arg1;
78
9f95a23c
TL
79 /* Ensure all elements of the trace entry are visible to outside trace tools */
80 spdk_smp_wmb();
7c673cae 81 lcore_history->next_entry++;
7c673cae
FG
82}
83
11fdf7f2 84int
9f95a23c 85spdk_trace_init(const char *shm_name, uint64_t num_entries)
7c673cae 86{
7c673cae 87 int i = 0;
9f95a23c
TL
88 int histories_size;
89 uint64_t lcore_offsets[SPDK_TRACE_MAX_LCORE + 1];
90
f67539c2
TL
91 /* 0 entries requested - skip trace initialization */
92 if (num_entries == 0) {
93 return 0;
94 }
95
9f95a23c
TL
96 lcore_offsets[0] = sizeof(struct spdk_trace_flags);
97 for (i = 1; i < (int)SPDK_COUNTOF(lcore_offsets); i++) {
98 lcore_offsets[i] = spdk_get_trace_history_size(num_entries) + lcore_offsets[i - 1];
99 }
f67539c2 100 histories_size = lcore_offsets[SPDK_TRACE_MAX_LCORE];
7c673cae
FG
101
102 snprintf(g_shm_name, sizeof(g_shm_name), "%s", shm_name);
103
11fdf7f2
TL
104 g_trace_fd = shm_open(shm_name, O_RDWR | O_CREAT, 0600);
105 if (g_trace_fd == -1) {
f67539c2
TL
106 SPDK_ERRLOG("could not shm_open spdk_trace\n");
107 SPDK_ERRLOG("errno=%d %s\n", errno, spdk_strerror(errno));
11fdf7f2 108 return 1;
7c673cae
FG
109 }
110
9f95a23c 111 if (ftruncate(g_trace_fd, histories_size) != 0) {
f67539c2 112 SPDK_ERRLOG("could not truncate shm\n");
11fdf7f2 113 goto trace_init_err;
7c673cae
FG
114 }
115
9f95a23c 116 g_trace_histories = mmap(NULL, histories_size, PROT_READ | PROT_WRITE,
11fdf7f2
TL
117 MAP_SHARED, g_trace_fd, 0);
118 if (g_trace_histories == MAP_FAILED) {
f67539c2 119 SPDK_ERRLOG("could not mmap shm\n");
11fdf7f2
TL
120 goto trace_init_err;
121 }
122
123 /* TODO: On FreeBSD, mlock on shm_open'd memory doesn't seem to work. Docs say that kern.ipc.shm_use_phys=1
124 * should allow it, but forcing that doesn't seem to work either. So for now just skip mlock on FreeBSD
125 * altogether.
126 */
127#if defined(__linux__)
9f95a23c 128 if (mlock(g_trace_histories, histories_size) != 0) {
f67539c2 129 SPDK_ERRLOG("Could not mlock shm for tracing - %s.\n", spdk_strerror(errno));
11fdf7f2 130 if (errno == ENOMEM) {
f67539c2 131 SPDK_ERRLOG("Check /dev/shm for old tracing files that can be deleted.\n");
11fdf7f2
TL
132 }
133 goto trace_init_err;
7c673cae 134 }
11fdf7f2 135#endif
7c673cae 136
9f95a23c 137 memset(g_trace_histories, 0, histories_size);
7c673cae 138
11fdf7f2
TL
139 g_trace_flags = &g_trace_histories->flags;
140
141 g_trace_flags->tsc_rate = spdk_get_ticks_hz();
7c673cae
FG
142
143 for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
9f95a23c
TL
144 struct spdk_trace_history *lcore_history;
145
146 g_trace_flags->lcore_history_offsets[i] = lcore_offsets[i];
147 lcore_history = spdk_get_per_lcore_history(g_trace_histories, i);
148 lcore_history->lcore = i;
149 lcore_history->num_entries = num_entries;
7c673cae 150 }
9f95a23c 151 g_trace_flags->lcore_history_offsets[SPDK_TRACE_MAX_LCORE] = lcore_offsets[SPDK_TRACE_MAX_LCORE];
7c673cae 152
11fdf7f2 153 spdk_trace_flags_init();
7c673cae 154
11fdf7f2 155 return 0;
7c673cae 156
11fdf7f2
TL
157trace_init_err:
158 if (g_trace_histories != MAP_FAILED) {
9f95a23c 159 munmap(g_trace_histories, histories_size);
11fdf7f2
TL
160 }
161 close(g_trace_fd);
162 g_trace_fd = -1;
163 shm_unlink(shm_name);
164 g_trace_histories = NULL;
7c673cae 165
11fdf7f2 166 return 1;
7c673cae 167
7c673cae
FG
168}
169
170void
11fdf7f2 171spdk_trace_cleanup(void)
7c673cae 172{
11fdf7f2 173 bool unlink;
9f95a23c
TL
174 int i;
175 struct spdk_trace_history *lcore_history;
7c673cae 176
11fdf7f2
TL
177 if (g_trace_histories == NULL) {
178 return;
179 }
7c673cae 180
11fdf7f2 181 /*
9f95a23c 182 * Only unlink the shm if there were no trace_entry recorded. This ensures the file
11fdf7f2
TL
183 * can be used after this process exits/crashes for debugging.
184 * Note that we have to calculate this value before g_trace_histories gets unmapped.
185 */
9f95a23c
TL
186 for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
187 lcore_history = spdk_get_per_lcore_history(g_trace_histories, i);
188 unlink = lcore_history->entries[0].tsc == 0;
189 if (!unlink) {
190 break;
191 }
192 }
193
11fdf7f2
TL
194 munmap(g_trace_histories, sizeof(struct spdk_trace_histories));
195 g_trace_histories = NULL;
196 close(g_trace_fd);
7c673cae 197
11fdf7f2
TL
198 if (unlink) {
199 shm_unlink(g_shm_name);
200 }
7c673cae 201}