|
Packit Service |
310c69 |
/*
|
|
Packit Service |
310c69 |
* Copyright (c) 2020 Red Hat, Inc.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* This program is free software; you can redistribute it and/or
|
|
Packit Service |
310c69 |
* modify it under the terms of the GNU General Public License
|
|
Packit Service |
310c69 |
* as published by the Free Software Foundation; either version 2
|
|
Packit Service |
310c69 |
* of the License, or (at your option) any later version.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
310c69 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
310c69 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
310c69 |
* GNU General Public License for more details.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* You should have received a copy of the GNU General Public License
|
|
Packit Service |
310c69 |
* along with this program; if not, write to the Free Software
|
|
Packit Service |
310c69 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
Packit Service |
310c69 |
* 02110-1301, USA.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* $Id: //eng/vdo-releases/aluminum/src/c++/vdo/kernel/ktrace.c#2 $
|
|
Packit Service |
310c69 |
*/
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#include "ktrace.h"
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#include "memoryAlloc.h"
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#include "dataVIO.h"
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#include "kvio.h"
|
|
Packit Service |
310c69 |
#include "logger.h"
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
enum {
|
|
Packit Service |
310c69 |
// How much data from a trace can we log in one call without messing
|
|
Packit Service |
310c69 |
// up the log or losing data?
|
|
Packit Service |
310c69 |
TRACE_LOG_MAX = 820,
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
// What fraction (1 out of TRACE_SAMPLE_INTERVAL VIOs) to trace
|
|
Packit Service |
310c69 |
TRACE_SAMPLE_INTERVAL = 3,
|
|
Packit Service |
310c69 |
};
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
bool traceRecording = false;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
static struct {
|
|
Packit Service |
310c69 |
char buffer[2000];
|
|
Packit Service |
310c69 |
unsigned int counter;
|
|
Packit Service |
310c69 |
struct mutex lock;
|
|
Packit Service |
310c69 |
} traceLoggingState;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Initialize a SampleCounter structure with the given sampling interval.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param counter The counter to initialize
|
|
Packit Service |
310c69 |
* @param interval The desired sampling interval
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static void initializeSampleCounter(SampleCounter *counter,
|
|
Packit Service |
310c69 |
unsigned int interval)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
spin_lock_init(&counter->lock);
|
|
Packit Service |
310c69 |
counter->tick = 0;
|
|
Packit Service |
310c69 |
counter->interval = interval;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*************************************************************************/
|
|
Packit Service |
310c69 |
bool sampleThisOne(SampleCounter *counter)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
bool wantTracing = false;
|
|
Packit Service |
310c69 |
spin_lock(&counter->lock);
|
|
Packit Service |
310c69 |
counter->tick++;
|
|
Packit Service |
310c69 |
if (counter->tick >= counter->interval) {
|
|
Packit Service |
310c69 |
counter->tick = 0;
|
|
Packit Service |
310c69 |
wantTracing = true;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
spin_unlock(&counter->lock);
|
|
Packit Service |
310c69 |
return wantTracing;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*************************************************************************/
|
|
Packit Service |
310c69 |
static void freeTraceDataBuffer(void *poolData, void *data)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
Trace *trace = (Trace *) data;
|
|
Packit Service |
310c69 |
FREE(trace);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*************************************************************************/
|
|
Packit Service |
310c69 |
static int allocTraceDataBuffer(void *poolData, void **dataPtr)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
Trace *trace;
|
|
Packit Service |
310c69 |
int result = ALLOCATE(1, Trace, __func__, &trace);
|
|
Packit Service |
310c69 |
if (result != VDO_SUCCESS) {
|
|
Packit Service |
310c69 |
logError("trace data allocation failure %d", result);
|
|
Packit Service |
310c69 |
return result;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
*dataPtr = trace;
|
|
Packit Service |
310c69 |
return VDO_SUCCESS;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*************************************************************************/
|
|
Packit Service |
310c69 |
int allocTraceFromPool(KernelLayer *layer, Trace **tracePointer)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
int result = allocBufferFromPool(layer->traceBufferPool,
|
|
Packit Service |
310c69 |
(void **) tracePointer);
|
|
Packit Service |
310c69 |
if (result == VDO_SUCCESS) {
|
|
Packit Service |
310c69 |
(*tracePointer)->used = 0;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
return result;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*************************************************************************/
|
|
Packit Service |
310c69 |
void freeTraceToPool(KernelLayer *layer, Trace *trace)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
freeBufferToPool(layer->traceBufferPool, trace);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*************************************************************************/
|
|
Packit Service |
310c69 |
int traceKernelLayerInit(KernelLayer *layer)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
layer->vioTraceRecording = traceRecording;
|
|
Packit Service |
310c69 |
initializeSampleCounter(&layer->traceSampleCounter, TRACE_SAMPLE_INTERVAL);
|
|
Packit Service |
310c69 |
unsigned int traceRecordsNeeded = 0;
|
|
Packit Service |
310c69 |
if (layer->vioTraceRecording) {
|
|
Packit Service |
310c69 |
traceRecordsNeeded += layer->requestLimiter.limit;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
if (traceRecordsNeeded > 0) {
|
|
Packit Service |
310c69 |
return makeBufferPool("KVDO Trace Data Pool", traceRecordsNeeded,
|
|
Packit Service |
310c69 |
allocTraceDataBuffer, freeTraceDataBuffer, NULL,
|
|
Packit Service |
310c69 |
layer, &layer->traceBufferPool);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
return VDO_SUCCESS;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*************************************************************************/
|
|
Packit Service |
310c69 |
void initializeTraceLoggingOnce(void)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
mutex_init(&traceLoggingState.lock);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*************************************************************************/
|
|
Packit Service |
310c69 |
void logKvioTrace(KVIO *kvio)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
KernelLayer *layer = kvio->layer;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
mutex_lock(&traceLoggingState.lock);
|
|
Packit Service |
310c69 |
traceLoggingState.counter++;
|
|
Packit Service |
310c69 |
// Log about 0.1% to avoid spewing data faster than syslog can keep up
|
|
Packit Service |
310c69 |
// (on certain of Permabit's test machines).
|
|
Packit Service |
310c69 |
// Yes, the 37 is arbitrary and meaningless.
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
if (layer->traceLogging && ((traceLoggingState.counter % 1024) == 37)) {
|
|
Packit Service |
310c69 |
kvioAddTraceRecord(kvio, THIS_LOCATION(NULL));
|
|
Packit Service |
310c69 |
size_t traceLen = 0;
|
|
Packit Service |
310c69 |
formatTrace(kvio->vio->trace, traceLoggingState.buffer,
|
|
Packit Service |
310c69 |
sizeof(traceLoggingState.buffer), &traceLen);
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
if (isMetadata(kvio)) {
|
|
Packit Service |
310c69 |
logInfo("finishing kvio %s meta @%" PRIptr " %s",
|
|
Packit Service |
310c69 |
(isWriteVIO(kvio->vio) ? "read" : "write"),
|
|
Packit Service |
310c69 |
kvio, traceLoggingState.buffer);
|
|
Packit Service |
310c69 |
} else if (isCompressedWriter(kvio)) {
|
|
Packit Service |
310c69 |
logInfo("finishing kvio write comp @%" PRIptr " %s",
|
|
Packit Service |
310c69 |
kvio, traceLoggingState.buffer);
|
|
Packit Service |
310c69 |
} else {
|
|
Packit Service |
310c69 |
const char *dupeLabel = "";
|
|
Packit Service |
310c69 |
if (isWriteVIO(kvio->vio)) {
|
|
Packit Service |
310c69 |
DataVIO *dataVIO = vioAsDataVIO(kvio->vio);
|
|
Packit Service |
310c69 |
if (isTrimDataVIO(dataVIO)) {
|
|
Packit Service |
310c69 |
dupeLabel = "trim ";
|
|
Packit Service |
310c69 |
} else if (dataVIO->isZeroBlock) {
|
|
Packit Service |
310c69 |
dupeLabel = "zero ";
|
|
Packit Service |
310c69 |
} else if (dataVIO->isDuplicate) {
|
|
Packit Service |
310c69 |
dupeLabel = "dupe ";
|
|
Packit Service |
310c69 |
} else {
|
|
Packit Service |
310c69 |
dupeLabel = "new ";
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
logInfo("finishing kvio %s data %s@%" PRIptr " %.*s",
|
|
Packit Service |
310c69 |
(isWriteVIO(kvio->vio) ? "read" : "write"),
|
|
Packit Service |
310c69 |
dupeLabel, kvio, TRACE_LOG_MAX, traceLoggingState.buffer);
|
|
Packit Service |
310c69 |
char *buf = traceLoggingState.buffer;
|
|
Packit Service |
310c69 |
while (traceLen > TRACE_LOG_MAX) {
|
|
Packit Service |
310c69 |
traceLen -= TRACE_LOG_MAX;
|
|
Packit Service |
310c69 |
buf += TRACE_LOG_MAX;
|
|
Packit Service |
310c69 |
logInfo("more kvio %" PRIptr " path: %.*s", kvio, TRACE_LOG_MAX, buf);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
mutex_unlock(&traceLoggingState.lock);
|
|
Packit Service |
310c69 |
}
|