162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef __TRACE_AGENT_H__
362306a36Sopenharmony_ci#define __TRACE_AGENT_H__
462306a36Sopenharmony_ci#include <pthread.h>
562306a36Sopenharmony_ci#include <stdbool.h>
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#define MAX_CPUS	256
862306a36Sopenharmony_ci#define PIPE_INIT       (1024*1024)
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci/*
1162306a36Sopenharmony_ci * agent_info - structure managing total information of guest agent
1262306a36Sopenharmony_ci * @pipe_size:	size of pipe (default 1MB)
1362306a36Sopenharmony_ci * @use_stdout:	set to true when o option is added (default false)
1462306a36Sopenharmony_ci * @cpus:	total number of CPUs
1562306a36Sopenharmony_ci * @ctl_fd:	fd of control path, /dev/virtio-ports/agent-ctl-path
1662306a36Sopenharmony_ci * @rw_ti:	structure managing information of read/write threads
1762306a36Sopenharmony_ci */
1862306a36Sopenharmony_cistruct agent_info {
1962306a36Sopenharmony_ci	unsigned long pipe_size;
2062306a36Sopenharmony_ci	bool use_stdout;
2162306a36Sopenharmony_ci	int cpus;
2262306a36Sopenharmony_ci	int ctl_fd;
2362306a36Sopenharmony_ci	struct rw_thread_info *rw_ti[MAX_CPUS];
2462306a36Sopenharmony_ci};
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci/*
2762306a36Sopenharmony_ci * rw_thread_info - structure managing a read/write thread a cpu
2862306a36Sopenharmony_ci * @cpu_num:	cpu number operating this read/write thread
2962306a36Sopenharmony_ci * @in_fd:	fd of reading trace data path in cpu_num
3062306a36Sopenharmony_ci * @out_fd:	fd of writing trace data path in cpu_num
3162306a36Sopenharmony_ci * @read_pipe:	fd of read pipe
3262306a36Sopenharmony_ci * @write_pipe:	fd of write pipe
3362306a36Sopenharmony_ci * @pipe_size:	size of pipe (default 1MB)
3462306a36Sopenharmony_ci */
3562306a36Sopenharmony_cistruct rw_thread_info {
3662306a36Sopenharmony_ci	int cpu_num;
3762306a36Sopenharmony_ci	int in_fd;
3862306a36Sopenharmony_ci	int out_fd;
3962306a36Sopenharmony_ci	int read_pipe;
4062306a36Sopenharmony_ci	int write_pipe;
4162306a36Sopenharmony_ci	unsigned long pipe_size;
4262306a36Sopenharmony_ci};
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci/* use for stopping rw threads */
4562306a36Sopenharmony_ciextern bool global_sig_receive;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci/* use for notification */
4862306a36Sopenharmony_ciextern bool global_run_operation;
4962306a36Sopenharmony_ciextern pthread_mutex_t mutex_notify;
5062306a36Sopenharmony_ciextern pthread_cond_t cond_wakeup;
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci/* for controller of read/write threads */
5362306a36Sopenharmony_ciextern int rw_ctl_init(const char *ctl_path);
5462306a36Sopenharmony_ciextern void *rw_ctl_loop(int ctl_fd);
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci/* for trace read/write thread */
5762306a36Sopenharmony_ciextern void *rw_thread_info_new(void);
5862306a36Sopenharmony_ciextern void *rw_thread_init(int cpu, const char *in_path, const char *out_path,
5962306a36Sopenharmony_ci			bool stdout_flag, unsigned long pipe_size,
6062306a36Sopenharmony_ci			struct rw_thread_info *rw_ti);
6162306a36Sopenharmony_ciextern pthread_t rw_thread_run(struct rw_thread_info *rw_ti);
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_cistatic inline void *zalloc(size_t size)
6462306a36Sopenharmony_ci{
6562306a36Sopenharmony_ci	return calloc(1, size);
6662306a36Sopenharmony_ci}
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#define pr_err(format, ...) fprintf(stderr, format, ## __VA_ARGS__)
6962306a36Sopenharmony_ci#define pr_info(format, ...) fprintf(stdout, format, ## __VA_ARGS__)
7062306a36Sopenharmony_ci#ifdef DEBUG
7162306a36Sopenharmony_ci#define pr_debug(format, ...) fprintf(stderr, format, ## __VA_ARGS__)
7262306a36Sopenharmony_ci#else
7362306a36Sopenharmony_ci#define pr_debug(format, ...) do {} while (0)
7462306a36Sopenharmony_ci#endif
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci#endif /*__TRACE_AGENT_H__*/
77