Skip to content

Commit e63aef9

Browse files
tlebbroonie
authored andcommitted
spi: spi-mem: add statistics support to ->exec_op() calls
Current behavior is that spi-mem operations do not increment statistics, neither per-controller nor per-device, if ->exec_op() is used. For operations that do NOT use ->exec_op(), stats are increased as the usual spi_sync() is called. The newly implemented spi_mem_add_op_stats() function is strongly inspired by spi_statistics_add_transfer_stats(); locking logic and l2len computation comes from there. Statistics that are being filled: bytes{,_rx,_tx}, messages, transfers, errors, timedout, transfer_bytes_histo_*. Note about messages & transfers counters: in the fallback to spi_sync() case, there are from 1 to 4 transfers per message. We only register one big transfer in the ->exec_op() case as that is closer to reality. This patch is NOT touching: - spi_async, spi_sync, spi_sync_immediate: those counters describe precise function calls, incrementing them would be lying. I believe comparing the messages counter to spi_async+spi_sync is a good way to detect ->exec_op() calls, but I might be missing edge cases knowledge. - transfers_split_maxsize: splitting cannot happen if ->exec_op() is provided. Reviewed-by: Dhruva Gole <[email protected]> Signed-off-by: Théo Lebrun <[email protected]> Reviewed-by: Tudor Ambarus <[email protected]> Link: https://msgid.link/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 0f3841a commit e63aef9

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

drivers/spi/spi-mem.c

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,49 @@ static void spi_mem_access_end(struct spi_mem *mem)
297297
pm_runtime_put(ctlr->dev.parent);
298298
}
299299

300+
static void spi_mem_add_op_stats(struct spi_statistics __percpu *pcpu_stats,
301+
const struct spi_mem_op *op, int exec_op_ret)
302+
{
303+
struct spi_statistics *stats;
304+
u64 len, l2len;
305+
306+
get_cpu();
307+
stats = this_cpu_ptr(pcpu_stats);
308+
u64_stats_update_begin(&stats->syncp);
309+
310+
/*
311+
* We do not have the concept of messages or transfers. Let's consider
312+
* that one operation is equivalent to one message and one transfer.
313+
*/
314+
u64_stats_inc(&stats->messages);
315+
u64_stats_inc(&stats->transfers);
316+
317+
/* Use the sum of all lengths as bytes count and histogram value. */
318+
len = op->cmd.nbytes + op->addr.nbytes;
319+
len += op->dummy.nbytes + op->data.nbytes;
320+
u64_stats_add(&stats->bytes, len);
321+
l2len = min(fls(len), SPI_STATISTICS_HISTO_SIZE) - 1;
322+
u64_stats_inc(&stats->transfer_bytes_histo[l2len]);
323+
324+
/* Only account for data bytes as transferred bytes. */
325+
if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT)
326+
u64_stats_add(&stats->bytes_tx, op->data.nbytes);
327+
if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN)
328+
u64_stats_add(&stats->bytes_rx, op->data.nbytes);
329+
330+
/*
331+
* A timeout is not an error, following the same behavior as
332+
* spi_transfer_one_message().
333+
*/
334+
if (exec_op_ret == -ETIMEDOUT)
335+
u64_stats_inc(&stats->timedout);
336+
else if (exec_op_ret)
337+
u64_stats_inc(&stats->errors);
338+
339+
u64_stats_update_end(&stats->syncp);
340+
put_cpu();
341+
}
342+
300343
/**
301344
* spi_mem_exec_op() - Execute a memory operation
302345
* @mem: the SPI memory
@@ -339,8 +382,12 @@ int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
339382
* read path) and expect the core to use the regular SPI
340383
* interface in other cases.
341384
*/
342-
if (!ret || ret != -ENOTSUPP || ret != -EOPNOTSUPP)
385+
if (!ret || ret != -ENOTSUPP || ret != -EOPNOTSUPP) {
386+
spi_mem_add_op_stats(ctlr->pcpu_statistics, op, ret);
387+
spi_mem_add_op_stats(mem->spi->pcpu_statistics, op, ret);
388+
343389
return ret;
390+
}
344391
}
345392

346393
tmpbufsize = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;

0 commit comments

Comments
 (0)