lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Sat, 17 Sep 2022 16:23:49 +0900
From:   Jiho Chu <jiho.chu@...sung.com>
To:     gregkh@...uxfoundation.org, arnd@...db.de, ogabbay@...nel.org,
        krzysztof.kozlowski@...aro.org, broonie@...nel.org
Cc:     linux-kernel@...r.kernel.org, yelini.jeong@...sung.com,
        myungjoo.ham@...sung.com, jiho.chu@...sung.com
Subject: [PATCH v2 06/13] trinity: add statistics module

This patch includes statistics information module.

The information includes per-application statistics and per-request
statistics. The app statistics records total number of requests,
active requests, allocated memory and freed memory.
For request statistics, it counts number of runs and total consumed
time, and it also keeps profile data for the requests.

Signed-off-by: Jiho Chu <jiho.chu@...sung.com>
---
 drivers/misc/trinity/Makefile       |   1 +
 drivers/misc/trinity/trinity.c      |   3 +
 drivers/misc/trinity/trinity_stat.c | 898 ++++++++++++++++++++++++++++
 drivers/misc/trinity/trinity_stat.h |  56 ++
 4 files changed, 958 insertions(+)
 create mode 100644 drivers/misc/trinity/trinity_stat.c
 create mode 100644 drivers/misc/trinity/trinity_stat.h

diff --git a/drivers/misc/trinity/Makefile b/drivers/misc/trinity/Makefile
index 5d3e89dd0dd7..b475938a0db6 100644
--- a/drivers/misc/trinity/Makefile
+++ b/drivers/misc/trinity/Makefile
@@ -6,5 +6,6 @@ trinity-y := trinity.o
 trinity-y += trinity_dma.o trinity_hwmem.o
 trinity-y += trinity_sched.o
 trinity-y += trinity_debug.o
+trinity-y += trinity_stat.o
 
 trinity_vision2-objs := $(trinity-y) trinity_vision2_drv.o
diff --git a/drivers/misc/trinity/trinity.c b/drivers/misc/trinity/trinity.c
index 0c75eb13967c..a785a5dca4d9 100644
--- a/drivers/misc/trinity/trinity.c
+++ b/drivers/misc/trinity/trinity.c
@@ -15,6 +15,7 @@
 
 #include "trinity_common.h"
 #include "trinity_sched.h"
+#include "trinity_stat.h"
 
 #define TRINITY_PADDR_BASE (0x0)
 
@@ -100,6 +101,8 @@ int trinity_open(struct inode *inode, struct file *f)
 	drv = container_of(miscdev, struct trinity_driver, mdev);
 	f->private_data = drv;
 
+	trinity_stat_app_set_status(drv, TRINITY_APP_STATUS_STARTED);
+
 	return 0;
 }
 
diff --git a/drivers/misc/trinity/trinity_stat.c b/drivers/misc/trinity/trinity_stat.c
new file mode 100644
index 000000000000..0cbba08ee0b0
--- /dev/null
+++ b/drivers/misc/trinity/trinity_stat.c
@@ -0,0 +1,898 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Providing statistics for Samsung Trinity device family support
+ *
+ * Copyright (C) 2021-2022 Samsung Electronics
+ * Copyright (C) 2021 Dongju Chae <dongju.chae@...sung.com>
+ * Copyright (C) 2022 MyungJoo Ham <myungjoo.ham@...sung.com>
+ * Copyright (C) 2022 Yelin Jeong <yelini.jeong@...sung.com>
+ * Copyright (C) 2022 Jiho Chu <jiho.chu@...sung.com>
+ */
+
+#include "trinity_stat.h"
+#include "trinity_common.h"
+
+#include <linux/bitmap.h>
+#include <linux/list_bl.h>
+
+/* maximum number of stats configurable from sysfs */
+#define TRINITY_STAT_MAX_APPS (128UL)
+#define TRINITY_STAT_MAX_REQS (4096UL)
+#define TRINITY_STAT_MAX_REQS_PER_APP (128UL)
+
+/* default number of stats */
+#define TRINITY_STAT_DEF_APPS (32UL)
+#define TRINITY_STAT_DEF_REQS (128UL)
+#define TRINITY_STAT_DEF_REQS_PER_APP (32UL)
+
+/**
+ * struct trinity_stat_pool - Statistics pool which maintain statistics for device
+ *
+ * @bitmap_app: bitmap for app
+ * @bitmap_req: bitmap for request
+ * @mem_app: reserved memory for applications
+ * @mem_req: reserved memory for request
+ * @max_stat_apps: max statistics size of applications
+ * @max_stat_reqs: max statistics size of requests.
+ * @max_stat_reqs_per_app: max statistics size of request per application
+ * @cur_stat_apps: current statistics for applications
+ * @cur_stat_reqs: current statistics for requests
+ * @drv: an instance of the trinity driver
+ */
+struct trinity_stat_pool {
+	DECLARE_BITMAP(bitmap_app, TRINITY_STAT_MAX_APPS);
+	DECLARE_BITMAP(bitmap_req, TRINITY_STAT_MAX_REQS);
+
+	struct trinity_dma mem_app;
+	struct trinity_dma mem_req;
+
+	unsigned long max_stat_apps;
+	unsigned long max_stat_reqs;
+	unsigned long max_stat_reqs_per_app;
+
+	unsigned long cur_stat_apps;
+	unsigned long cur_stat_reqs;
+
+	struct trinity_driver *drv;
+};
+
+/**
+ * trinity_stat_pool_init(): Initialize trinity statistics pool
+ *
+ * @drv: an instance of the trinity driver
+ *
+ * Returns: 0 on success. Otherwise, returns negative error.
+ */
+int trinity_stat_pool_init(struct trinity_driver *drv)
+{
+	struct trinity_stat_pool *pool;
+
+	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
+	if (!pool)
+		return -ENOMEM;
+
+	pool->drv = drv;
+
+	drv->stat.pdata = pool;
+
+	return 0;
+}
+
+/**
+ * trinity_stat_pool_init(): finish trinity statistics pool
+ *
+ * @drv: an instance of the trinity driver
+ */
+void trinity_stat_pool_fini(struct trinity_driver *drv)
+{
+	struct device *dev = drv_to_dev_ptr(drv);
+	struct trinity_stat_pool *pool = drv->stat.pdata;
+
+	if (!pool)
+		return;
+
+	trinity_dma_free(dev, &pool->mem_app);
+	trinity_dma_free(dev, &pool->mem_req);
+	kfree(pool);
+
+	drv->stat.pdata = NULL;
+}
+
+static void trinity_stat_pool_resize_apps(struct trinity_stat_pool *pool,
+					  unsigned long num_apps)
+{
+	struct device *dev = drv_to_dev_ptr(pool->drv);
+	struct trinity_dma mem;
+	unsigned long size;
+	int err;
+
+	if (num_apps > TRINITY_STAT_MAX_APPS) {
+		dev_err(dev, "The maximum number of stat apps: %lu",
+			TRINITY_STAT_MAX_APPS);
+		return;
+	}
+
+	size = sizeof(struct trinity_stat_app) * num_apps;
+	err = trinity_dma_alloc(dev, size, &mem);
+	if (err < 0) {
+		dev_warn(dev, "Unable to allocate stats for apps");
+		return;
+	}
+
+	trinity_dma_free(dev, &pool->mem_app);
+
+	bitmap_fill(pool->bitmap_app, TRINITY_STAT_MAX_APPS);
+	bitmap_zero(pool->bitmap_app, num_apps);
+
+	pool->max_stat_apps = num_apps;
+	pool->mem_app = mem;
+}
+
+static void trinity_stat_pool_resize_reqs(struct trinity_stat_pool *pool,
+					  unsigned long num_reqs)
+{
+	struct device *dev = drv_to_dev_ptr(pool->drv);
+	struct trinity_dma mem;
+	unsigned long size;
+	int err;
+
+	if (num_reqs > TRINITY_STAT_MAX_REQS) {
+		dev_err(dev, "The maximum number of stat reqs: %lu",
+			TRINITY_STAT_MAX_REQS);
+		return;
+	}
+
+	size = sizeof(struct trinity_stat_req) * num_reqs;
+	err = trinity_dma_alloc(dev, size, &mem);
+	if (err < 0) {
+		dev_warn(dev, "Unable to allocate stats for apps");
+		return;
+	}
+	trinity_dma_free(dev, &pool->mem_req);
+
+	bitmap_fill(pool->bitmap_req, TRINITY_STAT_MAX_REQS);
+	bitmap_zero(pool->bitmap_req, num_reqs);
+
+	pool->max_stat_reqs = num_reqs;
+	pool->mem_req = mem;
+}
+
+static struct trinity_stat_app *
+trinity_stat_pool_get_app(struct trinity_driver *drv)
+{
+	struct trinity_stat *stat = &drv->stat;
+	struct trinity_stat_pool *pool = stat->pdata;
+	struct trinity_stat_app *app = NULL;
+	unsigned long slot;
+	bool retried = false;
+
+	/* ensured that the lock is acquired */
+retry:
+	slot = find_first_zero_bit(pool->bitmap_app, TRINITY_STAT_MAX_APPS);
+	if (slot < TRINITY_STAT_MAX_APPS) {
+		app = &((struct trinity_stat_app *)pool->mem_app.addr)[slot];
+		memset(app, '\x00', sizeof(*app));
+		set_bit(slot, pool->bitmap_app);
+		app->slot = slot;
+	} else if (!retried) {
+		/* retry after destroy old stats */
+		retried = true;
+		trinity_destroy_stats(stat, true);
+		goto retry;
+	} else {
+		dev_warn(drv_to_dev_ptr(pool->drv),
+			 "Please increase stat pool limit for apps");
+	}
+
+	return app;
+}
+
+static void trinity_stat_pool_put_app(struct trinity_driver *drv,
+				      struct trinity_stat_app *app)
+{
+	struct trinity_stat_pool *pool = drv->stat.pdata;
+
+	/* ensured that the lock is acquired */
+	clear_bit(app->slot, pool->bitmap_app);
+}
+
+static struct trinity_stat_req *
+trinity_stat_pool_get_req(struct trinity_driver *drv)
+{
+	struct trinity_stat *stat = &drv->stat;
+	struct trinity_stat_pool *pool = stat->pdata;
+	struct trinity_stat_req *req = NULL;
+	unsigned long slot;
+	bool retried = false;
+
+	/* ensured that the lock is acquired */
+retry:
+	slot = find_first_zero_bit(pool->bitmap_req, TRINITY_STAT_MAX_REQS);
+	if (slot < TRINITY_STAT_MAX_REQS) {
+		req = &((struct trinity_stat_req *)pool->mem_req.addr)[slot];
+		memset(req, '\x00', sizeof(*req));
+		set_bit(slot, pool->bitmap_req);
+		req->slot = slot;
+	} else if (!retried) {
+		/* retry after destroy old stats */
+		retried = true;
+		trinity_destroy_stats(stat, true);
+		goto retry;
+	} else {
+		dev_warn(drv_to_dev_ptr(pool->drv),
+			 "Please increase stat pool limit for reqs");
+	}
+
+	return req;
+}
+
+static void trinity_stat_pool_put_req(struct trinity_driver *drv,
+				      struct trinity_stat_req *req)
+{
+	struct trinity_stat_pool *pool = drv->stat.pdata;
+
+	/* ensured that the lock is acquired */
+	clear_bit(req->slot, pool->bitmap_req);
+}
+
+/**
+ * trinity_stat_init(): Initialize trinity statistics
+ *
+ * @drv: an instance of the trinity driver
+ */
+void trinity_stat_init(struct trinity_driver *drv)
+{
+	unsigned long i;
+
+	spin_lock_init(&drv->stat.lock);
+
+	INIT_LIST_HEAD(&drv->stat.list);
+	for (i = 0; i < TRINITY_STAT_HASH_SIZE; ++i)
+		INIT_HLIST_BL_HEAD(&drv->stat.hlist[i]);
+
+	trinity_stat_pool_init(drv);
+	/* initialize to default values */
+	trinity_stat_resize(drv, TRINITY_STAT_DEF_APPS, TRINITY_STAT_DEF_REQS,
+			    TRINITY_STAT_DEF_REQS_PER_APP);
+}
+
+/**
+ * trinity_stat_fini(): Finish trinity statistics
+ *
+ * @drv: an instance of the trinity driver
+ */
+void trinity_stat_fini(struct trinity_driver *drv)
+{
+	trinity_stat_resize(drv, 0, 0, 0);
+	trinity_stat_pool_fini(drv);
+}
+
+/**
+ * trinity_stat_fini(): Finish trinity statistics
+ *
+ * @drv: an instance of the trinity driver
+ * @num_apps: a number of applications
+ * @num_reqs: a number of requests
+ * @num_reqs_per_app: a number of requests per application
+ */
+void trinity_stat_resize(struct trinity_driver *drv, unsigned long num_apps,
+			 unsigned long num_reqs, unsigned long num_reqs_per_app)
+{
+	struct trinity_stat *stat = &drv->stat;
+	struct trinity_stat_pool *pool = stat->pdata;
+	unsigned long i;
+
+	if (!pool)
+		return;
+
+	trinity_stat_lock(&drv->stat);
+
+	for (i = 0; i < TRINITY_STAT_HASH_SIZE; i++) {
+		struct trinity_stat_app *stat_app;
+		struct hlist_bl_node *hn;
+
+		hlist_bl_lock(&(stat->hlist[i]));
+		hlist_bl_for_each_entry(stat_app, hn, &(stat->hlist[i]),
+					 hnode) {
+			if (stat_app->status != TRINITY_APP_STATUS_TERMINATED) {
+				dev_warn(drv_to_dev_ptr(drv),
+					 "Still busy apps detected.. waiting");
+				hlist_bl_unlock(&(stat->hlist[i]));
+				goto unlock;
+			}
+		}
+		hlist_bl_unlock(&(stat->hlist[i]));
+	}
+
+	trinity_destroy_stats(stat, true);
+
+	/* re-allocate each stat buffer */
+	if (num_apps > 0)
+		trinity_stat_pool_resize_apps(pool, num_apps);
+
+	if (num_reqs > 0)
+		trinity_stat_pool_resize_reqs(pool, num_reqs);
+
+	if (num_reqs_per_app > 0)
+		pool->max_stat_reqs_per_app = num_reqs_per_app;
+
+unlock:
+	trinity_stat_unlock(&drv->stat);
+}
+
+/**
+ * trinity_stat_get_max_apps(): Get max statistics size for application
+ *
+ * @drv: an instance of the trinity driver
+ *
+ * Returns max number of statistics for applications. 0 on error.
+ */
+unsigned long trinity_stat_get_max_apps(struct trinity_driver *drv)
+{
+	struct trinity_stat_pool *pool = drv->stat.pdata;
+	unsigned long num;
+
+	if (!pool)
+		return 0;
+
+	trinity_stat_lock(&drv->stat);
+	num = pool->max_stat_apps;
+	trinity_stat_unlock(&drv->stat);
+
+	return num;
+}
+
+/**
+ * trinity_stat_get_max_reqs(): Get max statistics size for requests
+ *
+ * @drv: an instance of the trinity driver
+ *
+ * Returns max number of statistics for requests. 0 on error.
+ */
+unsigned long trinity_stat_get_max_reqs(struct trinity_driver *drv)
+{
+	struct trinity_stat_pool *pool = drv->stat.pdata;
+	unsigned long num;
+
+	if (!pool)
+		return 0;
+
+	trinity_stat_lock(&drv->stat);
+	num = pool->max_stat_reqs;
+	trinity_stat_unlock(&drv->stat);
+
+	return num;
+}
+
+/**
+ * trinity_stat_get_max_reqs(): Get max statistics size for requests per application
+ *
+ * @drv: an instance of the trinity driver
+ *
+ * Returns max number of statistics for requests per application. 0 on error.
+ */
+unsigned long trinity_stat_get_max_reqs_per_app(struct trinity_driver *drv)
+{
+	struct trinity_stat_pool *pool = drv->stat.pdata;
+	unsigned long num;
+
+	if (!pool)
+		return 0;
+
+	trinity_stat_lock(&drv->stat);
+	num = pool->max_stat_reqs_per_app;
+	trinity_stat_unlock(&drv->stat);
+
+	return num;
+}
+
+/**
+ * trinity_stat_lock(): Lock for trinity statistics
+ *
+ * @stat: an instance of trinity statistics
+ */
+void trinity_stat_lock(struct trinity_stat *stat)
+{
+	if (stat)
+		spin_lock(&stat->lock);
+}
+
+/**
+ * trinity_stat_unlock(): Unlock for trinity statistics
+ *
+ * @stat: an instance of trinity statistics
+ */
+void trinity_stat_unlock(struct trinity_stat *stat)
+{
+	if (stat)
+		spin_unlock(&stat->lock);
+}
+
+/**
+ * trinity_create_stat_app() - Create a stat structure for the opened app
+ *
+ * @drv: An instance of the trinity driver.
+ *
+ * Return: 0 on success. Otherwise, returns negative error.
+ */
+static int trinity_create_stat_app(struct trinity_driver *drv)
+{
+	struct trinity_stat *stat = &drv->stat;
+	struct trinity_stat_pool *pool = stat->pdata;
+	struct trinity_stat_app *stat_app;
+	unsigned long key;
+
+	trinity_stat_lock(stat);
+	stat_app = trinity_stat_pool_get_app(drv);
+	if (IS_ERR_OR_NULL(stat_app)) {
+		trinity_stat_unlock(stat);
+		dev_err(drv_to_dev_ptr(drv),
+			"Unable to allocate stat of request");
+		return -ENOMEM;
+	}
+
+	stat_app->parent = stat;
+	stat_app->app_id = trinity_get_app_id();
+	stat_app->total_alloc_mem = 0;
+	stat_app->total_freed_mem = 0;
+	stat_app->num_total_reqs = 0;
+	stat_app->num_kept_reqs = 0;
+	stat_app->num_active_reqs = 0;
+	stat_app->status = TRINITY_APP_STATUS_STARTED;
+
+	strncpy(stat_app->name, current->comm, TASK_COMM_LEN);
+	stat_app->name[TASK_COMM_LEN - 1] = '\x00';
+
+	INIT_HLIST_BL_NODE(&stat_app->hnode);
+	INIT_LIST_HEAD(&stat_app->reqs);
+
+	key = hash_long(stat_app->app_id, TRINITY_STAT_HASH_BITS);
+
+	hlist_bl_lock(&(stat->hlist[key]));
+	hlist_bl_add_head(&stat_app->hnode, &(stat->hlist[key]));
+	hlist_bl_unlock(&(stat->hlist[key]));
+
+	list_add_tail(&stat_app->lnode, &stat->list);
+	pool->cur_stat_apps++;
+
+	/* Remove terminated stats if the number reaches the maximum */
+	trinity_destroy_stats(stat, false);
+
+	trinity_stat_unlock(stat);
+
+	return 0;
+}
+
+static void trinity_destroy_stat_req(struct trinity_stat_req *stat_req)
+{
+	struct trinity_stat_app *stat_app = stat_req->parent;
+	struct trinity_stat *stat = stat_app->parent;
+	struct trinity_driver *drv =
+		container_of(stat, struct trinity_driver, stat);
+
+	if (stat_req->profile)
+		drv->desc->destroy_profile(drv, stat_req->profile);
+	list_del(&stat_req->list);
+	trinity_stat_pool_put_req(drv, stat_req);
+}
+
+static void trinity_destroy_stat_reqs(struct trinity_stat_app *stat_app)
+{
+	struct trinity_stat_req *stat_req, *tmp;
+
+	list_for_each_entry_safe(stat_req, tmp, &stat_app->reqs, list)
+		trinity_destroy_stat_req(stat_req);
+}
+
+/**
+ * trinity_destroy_stats - Destroy terminated stat structures
+ *
+ * @drv: An instance of the trinity driver
+ * @force: force destroy
+ */
+void trinity_destroy_stats(struct trinity_stat *stat, bool force)
+{
+	struct trinity_driver *drv =
+		container_of(stat, struct trinity_driver, stat);
+	struct trinity_stat_pool *pool = stat->pdata;
+	struct trinity_stat_app *stat_app;
+	struct hlist_bl_node *hn, *tmp;
+	int i;
+
+	/* lock should be acquired before */
+	if (!force && pool->cur_stat_apps <= pool->max_stat_apps)
+		return;
+
+	for (i = 0; i < TRINITY_STAT_HASH_SIZE; i++) {
+		hlist_bl_lock(&stat->hlist[i]);
+		hlist_bl_for_each_entry_safe(stat_app, hn, tmp,
+					      &(stat->hlist[i]), hnode) {
+			enum trinity_app_status status = stat_app->status;
+
+			if (status == TRINITY_APP_STATUS_TERMINATED) {
+				hlist_bl_del(&stat_app->hnode);
+				list_del(&stat_app->lnode);
+
+				pool->cur_stat_apps--;
+
+				trinity_destroy_stat_reqs(stat_app);
+				trinity_stat_pool_put_app(drv, stat_app);
+			}
+		}
+		hlist_bl_unlock(&stat->hlist[i]);
+	}
+}
+
+static struct trinity_stat_app *
+trinity_get_stat_by_id(struct trinity_driver *drv, int32_t app_id)
+{
+	struct trinity_stat *stat = &drv->stat;
+	struct trinity_stat_app *stat_app;
+	struct hlist_bl_node *hn;
+	unsigned long key;
+
+	key = hash_long(app_id, TRINITY_STAT_HASH_BITS);
+
+	hlist_bl_lock(&stat->hlist[key]);
+	hlist_bl_for_each_entry(stat_app, hn, &stat->hlist[key], hnode) {
+		if (stat_app->app_id == app_id)
+			goto out;
+	}
+	stat_app = NULL;
+out:
+	hlist_bl_unlock(&stat->hlist[key]);
+
+	return stat_app;
+}
+
+/**
+ * trinity_get_stat_app() - Get a status structure for the target app
+ *
+ * @drv: an instance of the trinity driver.
+ *
+ * Returns statistics for application on success. Otherwise, returns NULL.
+ *
+ * @note: If the stat is not allocated yet, try to create and return it.
+ */
+struct trinity_stat_app *trinity_get_stat_app(struct trinity_driver *drv)
+{
+	struct trinity_stat *stat = &drv->stat;
+	struct trinity_stat_app *stat_app;
+	int app_id = trinity_get_app_id();
+
+retry:
+	trinity_stat_lock(stat);
+	stat_app = trinity_get_stat_by_id(drv, app_id);
+	trinity_stat_unlock(stat);
+
+	if (!IS_ERR_OR_NULL(stat_app))
+		return stat_app;
+
+	if (trinity_create_stat_app(drv) != 0)
+		return NULL;
+
+	goto retry;
+}
+
+/**
+ * trinity_stat_app_set_status() - Set a status structure for the target app
+ *
+ * @drv: an instance of the trinity driver.
+ * @status: application status
+ */
+void trinity_stat_app_set_status(struct trinity_driver *drv,
+				 enum trinity_app_status status)
+{
+	struct trinity_stat *stat = &drv->stat;
+	struct trinity_stat_app *stat_app;
+	int app_id = trinity_get_app_id();
+
+	trinity_stat_lock(stat);
+	stat_app = trinity_get_stat_by_id(drv, app_id);
+	trinity_stat_unlock(stat);
+
+	if (IS_ERR_OR_NULL(stat_app))
+		return;
+
+	stat_app->status = status;
+}
+
+/**
+ * trinity_stat_append_req() - Append request information for statistics
+ *
+ * @drv: an instance of the trinity driver.
+ * @req: an instance of request
+ *
+ * Return: 0 on success. Otherwise, returns negative error.
+ */
+int trinity_stat_append_req(struct trinity_driver *drv, struct trinity_req *req)
+{
+	struct trinity_stat *stat = &drv->stat;
+	struct trinity_stat_pool *pool = stat->pdata;
+	struct trinity_stat_app *stat_app;
+	struct trinity_stat_req *stat_req;
+
+	stat_app = trinity_get_stat_app(drv);
+	if (IS_ERR_OR_NULL(stat_app))
+		return -ENOMEM;
+
+	trinity_stat_lock(stat);
+	stat_req = trinity_stat_pool_get_req(drv);
+	if (!stat_req) {
+		trinity_stat_unlock(stat);
+		dev_err(drv_to_dev_ptr(drv),
+			"Unable to allocate stat of request");
+		return -ENOMEM;
+	}
+
+	stat_req->parent = stat_app;
+	stat_req->app_id = stat_app->app_id;
+	stat_req->req_id = req->input.config.req_id;
+	stat_req->model_id = req->input.config.model_id;
+	stat_req->submitted = ktime_get();
+	stat_req->status = TRINITY_REQ_STATUS_PENDING;
+	stat_req->priority =
+		(enum trinity_req_priority)req->input.config.priority;
+	stat_req->is_kernel = req->is_kernel;
+
+	req->stat = stat_req;
+
+	list_add_tail(&stat_req->list, &stat_app->reqs);
+
+	/* don't count kernel requests */
+	if (!req->is_kernel) {
+		if (stat_app->num_kept_reqs == pool->max_stat_reqs_per_app) {
+			struct trinity_stat_req *old_stat;
+
+			old_stat = list_first_entry(
+				&stat_app->reqs, struct trinity_stat_req, list);
+			/* skip any kernel or unfinished request */
+			while (old_stat->is_kernel ||
+			       (old_stat->status !=
+					TRINITY_REQ_STATUS_FINISHED &&
+				old_stat->status != TRINITY_REQ_STATUS_ERROR))
+				old_stat = list_next_entry(old_stat, list);
+
+			WARN_ON(old_stat == NULL);
+
+			trinity_destroy_stat_req(old_stat);
+			stat_app->num_total_reqs--;
+		} else {
+			/* total number of user requests kepted */
+			stat_app->num_kept_reqs++;
+		}
+	}
+
+	stat_app->num_total_reqs++;
+	stat_app->num_active_reqs++;
+
+	trinity_stat_unlock(stat);
+	return 0;
+}
+
+/**
+ * trinity_stat_remove_req() - Remove request information for statistics
+ *
+ * @drv: an instance of the trinity driver.
+ * @req: an instance of the request to be used for statistics
+ * @rollback: rollback statistics
+ */
+void trinity_stat_remove_req(struct trinity_driver *drv,
+			     struct trinity_req *req, bool rollback)
+{
+	struct trinity_stat *stat = &drv->stat;
+	struct trinity_stat_req *stat_req = req->stat;
+	struct trinity_stat_app *stat_app = stat_req->parent;
+
+	trinity_stat_lock(stat);
+
+	trinity_destroy_stat_req(stat_req);
+
+	if (!req->is_kernel) {
+		WARN_ON(stat_app->num_kept_reqs == 0);
+		stat_app->num_kept_reqs--;
+	}
+
+	if (rollback) {
+		WARN_ON(stat_app->num_total_reqs == 0);
+		stat_app->num_total_reqs--;
+		WARN_ON(stat_app->num_active_reqs == 0);
+		stat_app->num_active_reqs--;
+	}
+
+	trinity_stat_unlock(stat);
+}
+
+/**
+ * trinity_stat_finish_req() - Finish request for statistics
+ *
+ * @drv: an instance of the trinity driver.
+ * @req: an instance of the request to be used for statistics
+ */
+void trinity_stat_finish_req(struct trinity_driver *drv,
+			     struct trinity_req *req)
+{
+	struct trinity_stat *stat = &drv->stat;
+	struct trinity_stat_req *stat_req = req->stat;
+	struct trinity_stat_app *stat_app = stat_req->parent;
+
+	trinity_stat_lock(stat);
+	if (stat_app->num_active_reqs != 0)
+		stat_app->num_active_reqs--;
+	else
+		dev_err(drv_to_dev_ptr(drv),
+			"Fail to keep track of the active reqs");
+	trinity_stat_unlock(stat);
+}
+
+static void copy_stat_app_ioctl(struct trinity_stat_app *stat_app,
+				struct trinity_ioctl_stat_app *ioctl_stat_app)
+{
+	ioctl_stat_app->app_id = stat_app->app_id;
+	ioctl_stat_app->status = stat_app->status;
+	ioctl_stat_app->num_total_reqs = stat_app->num_total_reqs;
+	ioctl_stat_app->num_active_reqs = stat_app->num_active_reqs;
+	ioctl_stat_app->total_alloc_mem = stat_app->total_alloc_mem;
+	ioctl_stat_app->total_freed_mem = stat_app->total_freed_mem;
+
+	strncpy(ioctl_stat_app->name, stat_app->name, TASK_COMM_LEN);
+	ioctl_stat_app->name[TASK_COMM_LEN - 1] = '\x00';
+}
+
+static void copy_stat_req_ioctl(struct trinity_stat_req *stat_req,
+				struct trinity_ioctl_stat_req *ioctl_stat_req)
+{
+	ktime_t cur_time = ktime_get();
+	ktime_t submitted, scheduled, completed;
+
+	submitted = stat_req->submitted;
+	scheduled = stat_req->scheduled ? stat_req->scheduled : cur_time;
+	completed = stat_req->completed ? stat_req->completed : cur_time;
+
+	ioctl_stat_req->req_id = stat_req->req_id;
+	ioctl_stat_req->model_id = stat_req->model_id;
+	ioctl_stat_req->priority = stat_req->priority;
+	ioctl_stat_req->status = stat_req->status;
+
+	if (stat_req->priority == TRINITY_REQ_PRIORITY_HIGH)
+		ioctl_stat_req->sched_time = 0;
+	else
+		ioctl_stat_req->sched_time = TIME_DIFF(scheduled, submitted);
+	ioctl_stat_req->infer_time = TIME_DIFF(completed, scheduled);
+}
+
+/**
+ * trinity_stat_app_copy_ioctl() - Copy an application's statistics information to ioctl info
+ *
+ * @drv: an instance of the trinity driver.
+ * @ioctl_stat_app: ioctl statistics information for an application
+ */
+void trinity_stat_app_copy_ioctl(struct trinity_driver *drv,
+				 struct trinity_ioctl_stat_app *ioctl_stat_app)
+{
+	struct trinity_stat *stat = &drv->stat;
+	struct trinity_stat_app *stat_app;
+	int app_id = trinity_get_app_id();
+
+	trinity_stat_lock(stat);
+
+	stat_app = trinity_get_stat_by_id(drv, app_id);
+	if (IS_ERR_OR_NULL(stat_app)) {
+		ioctl_stat_app->app_id = app_id;
+		ioctl_stat_app->status = TRINITY_APP_STATUS_PENDING;
+		ioctl_stat_app->num_total_reqs = 0;
+		ioctl_stat_app->num_active_reqs = 0;
+		ioctl_stat_app->total_alloc_mem = 0;
+		ioctl_stat_app->total_freed_mem = 0;
+
+		strncpy(ioctl_stat_app->name, current->comm, TASK_COMM_LEN);
+		ioctl_stat_app->name[TASK_COMM_LEN - 1] = '\x00';
+	} else {
+		copy_stat_app_ioctl(stat_app, ioctl_stat_app);
+	}
+
+	trinity_stat_unlock(stat);
+}
+
+/**
+ * trinity_stat_apps_copy_ioctl() - Copy applications' statistics information to ioctl info
+ *
+ * @drv: an instance of the trinity driver.
+ * @ioctl_stat_apps: ioctl statistics information for applications
+ */
+void trinity_stat_apps_copy_ioctl(
+	struct trinity_driver *drv,
+	struct trinity_ioctl_stat_apps *ioctl_stat_apps)
+{
+	struct trinity_stat *stat = &drv->stat;
+	struct trinity_ioctl_stat_app *ioctl_stat_app;
+	struct trinity_stat_app *stat_app;
+	uint32_t idx = 0;
+
+	trinity_stat_lock(stat);
+
+	list_for_each_entry(stat_app, &stat->list, lnode) {
+		if (idx >= TRINITY_APP_STAT_MAX)
+			break;
+		ioctl_stat_app = &ioctl_stat_apps->stat[idx++];
+		copy_stat_app_ioctl(stat_app, ioctl_stat_app);
+	}
+	ioctl_stat_apps->num_apps = idx;
+
+	trinity_stat_unlock(stat);
+}
+
+/**
+ * trinity_stat_app_copy_ioctl() - Copy requests' statistics information to ioctl info
+ *
+ * @drv: an instance of the trinity driver.
+ * @ioctl_stat_reqs: ioctl statistics information for requests
+ */
+void trinity_stat_reqs_copy_ioctl(
+	struct trinity_driver *drv,
+	struct trinity_ioctl_stat_reqs *ioctl_stat_reqs)
+{
+	struct trinity_stat *stat = &drv->stat;
+	struct trinity_ioctl_stat_req *ioctl_stat_req;
+	struct trinity_stat_app *stat_app;
+	struct trinity_stat_req *stat_req;
+	uint32_t idx = 0;
+
+	trinity_stat_lock(stat);
+	stat_app = trinity_get_stat_by_id(drv, ioctl_stat_reqs->app_id);
+	if (IS_ERR_OR_NULL(stat_app)) {
+		ioctl_stat_reqs->num_reqs = 0;
+		trinity_stat_unlock(stat);
+		return;
+	}
+
+	list_for_each_entry(stat_req, &stat_app->reqs, list) {
+		if (idx >= TRINITY_REQ_STAT_MAX)
+			break;
+		ioctl_stat_req = &ioctl_stat_reqs->stat[idx++];
+		copy_stat_req_ioctl(stat_req, ioctl_stat_req);
+	}
+	ioctl_stat_reqs->num_reqs = idx;
+
+	trinity_stat_unlock(stat);
+}
+
+/**
+ * trinity_stat_app_total_alloc() - Append allocated size to application's total memory size
+ *
+ * @drv: an instance of the trinity driver.
+ * @size: allocated memory size
+ */
+void trinity_stat_app_total_alloc(struct trinity_driver *drv, size_t size)
+{
+	struct trinity_stat *stat = &drv->stat;
+	struct trinity_stat_app *stat_app;
+
+	stat_app = trinity_get_stat_app(drv);
+	if (IS_ERR_OR_NULL(stat_app))
+		return;
+
+	trinity_stat_lock(stat);
+	stat_app->total_alloc_mem += size;
+	trinity_stat_unlock(stat);
+}
+
+/**
+ * trinity_stat_app_total_alloc() - Append freed size to application's total memory size
+ *
+ * @drv: an instance of the trinity driver.
+ * @size: freed memory size
+ */
+void trinity_stat_app_total_freed(struct trinity_driver *drv, size_t size)
+{
+	struct trinity_stat *stat = &drv->stat;
+	struct trinity_stat_app *stat_app;
+
+	stat_app = trinity_get_stat_app(drv);
+	if (IS_ERR_OR_NULL(stat_app))
+		return;
+
+	trinity_stat_lock(stat);
+	stat_app->total_freed_mem += size;
+	trinity_stat_unlock(stat);
+}
diff --git a/drivers/misc/trinity/trinity_stat.h b/drivers/misc/trinity/trinity_stat.h
new file mode 100644
index 000000000000..8ae02769efa0
--- /dev/null
+++ b/drivers/misc/trinity/trinity_stat.h
@@ -0,0 +1,56 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/**
+ * Statistics header for trinity devices
+ *
+ * Copyright (C) 2021-2022 Samsung Electronics
+ * Copyright (C) 2021 Dongju Chae <dongju.chae@...sung.com>
+ * Copyright (C) 2022 MyungJoo Ham <myungjoo.ham@...sung.com>
+ * Copyright (C) 2022 Yelin Jeong <yelini.jeong@...sung.com>
+ * Copyright (C) 2022 Jiho Chu <jiho.chu@...sung.com>
+ */
+
+#ifndef __DRIVERS_MISC_TRINITY_STAT_H__
+#define __DRIVERS_MISC_TRINITY_STAT_H__
+
+#include "trinity_common.h"
+
+void trinity_stat_init(struct trinity_driver *drv);
+void trinity_stat_fini(struct trinity_driver *drv);
+void trinity_stat_resize(struct trinity_driver *drv, unsigned long num_apps,
+			 unsigned long num_reqs,
+			 unsigned long num_reqs_per_app);
+
+void trinity_stat_lock(struct trinity_stat *stat);
+void trinity_stat_unlock(struct trinity_stat *stat);
+void trinity_destroy_stats(struct trinity_stat *stat, bool force);
+
+unsigned long trinity_stat_get_max_apps(struct trinity_driver *drv);
+unsigned long trinity_stat_get_max_reqs(struct trinity_driver *drv);
+unsigned long trinity_stat_get_max_reqs_per_app(struct trinity_driver *drv);
+
+struct trinity_stat_app *trinity_get_stat_app(struct trinity_driver *drv);
+
+void trinity_stat_app_total_alloc(struct trinity_driver *drv, size_t size);
+void trinity_stat_app_total_freed(struct trinity_driver *drv, size_t size);
+void trinity_stat_app_set_status(struct trinity_driver *drv,
+				 enum trinity_app_status status);
+
+int trinity_stat_append_req(struct trinity_driver *drv,
+			    struct trinity_req *req);
+void trinity_stat_remove_req(struct trinity_driver *drv,
+			     struct trinity_req *req, bool rollback);
+void trinity_stat_finish_req(struct trinity_driver *drv,
+			     struct trinity_req *req);
+
+void trinity_stat_app_copy_ioctl(struct trinity_driver *drv,
+				 struct trinity_ioctl_stat_app *ioctl_stat_app);
+
+void trinity_stat_apps_copy_ioctl(
+	struct trinity_driver *drv,
+	struct trinity_ioctl_stat_apps *ioctl_stat_apps);
+
+void trinity_stat_reqs_copy_ioctl(
+	struct trinity_driver *drv,
+	struct trinity_ioctl_stat_reqs *ioctl_stat_reqs);
+
+#endif /* __DRIVERS_MISC_TRINITY_STAT_H__ */
-- 
2.25.1

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ