feat[mpp_lock]: Add spinlock timing statistic

Signed-off-by: Herman Chen <herman.chen@rock-chips.com>
Change-Id: Id63e28605853f07c3e557f01d8edffd67d7d1541
This commit is contained in:
Herman Chen
2023-04-24 16:54:38 +08:00
parent fa9998fd37
commit 12aefae5b1
2 changed files with 42 additions and 1 deletions

View File

@@ -48,9 +48,13 @@ extern "C" {
typedef struct {
RK_U32 lock;
RK_U32 debug;
RK_S64 count;
RK_S64 time;
} spinlock_t;
void mpp_spinlock_init(spinlock_t *lock);
void mpp_spinlock_deinit(spinlock_t *lock, const char *name);
void mpp_spinlock_lock(spinlock_t *lock);
void mpp_spinlock_unlock(spinlock_t *lock);
bool mpp_spinlock_trylock(spinlock_t *lock);

View File

@@ -16,8 +16,10 @@
#define MODULE_TAG "mpp_lock"
#include "mpp_env.h"
#include "mpp_log.h"
#include "mpp_lock.h"
#include "mpp_time.h"
#define LOCK_IDLE 0
#define LOCK_BUSY 1
@@ -25,14 +27,36 @@
void mpp_spinlock_init(spinlock_t *lock)
{
MPP_SYNC_CLR(&lock->lock);
lock->count = 0;
lock->time = 0;
mpp_env_get_u32("mpp_lock_debug", &lock->debug, 0);
}
void mpp_spinlock_lock(spinlock_t *lock)
{
RK_S64 time = 0;
if (lock->debug)
time = mpp_time();
while (!MPP_BOOL_CAS(&lock->lock, LOCK_IDLE, LOCK_BUSY)) {
asm("NOP");
asm("NOP");
}
if (lock->debug && time) {
lock->time += mpp_time() - time;
lock->count++;
}
}
void mpp_spinlock_deinit(spinlock_t *lock, const char *name)
{
if (lock->debug && lock->count) {
mpp_log("lock %s lock %lld times take time %lld avg %d", name,
lock->count, lock->time, (RK_S32)(lock->time / lock->count));
}
}
void mpp_spinlock_unlock(spinlock_t *lock)
@@ -42,5 +66,18 @@ void mpp_spinlock_unlock(spinlock_t *lock)
bool mpp_spinlock_trylock(spinlock_t *lock)
{
return MPP_BOOL_CAS(&lock->lock, LOCK_IDLE, LOCK_BUSY);
RK_S64 time = 0;
bool ret;
if (lock->debug)
time = mpp_time();
ret = MPP_BOOL_CAS(&lock->lock, LOCK_IDLE, LOCK_BUSY);
if (ret && lock->debug && time) {
lock->time += mpp_time() - time;
lock->count++;
}
return ret;
}