18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _PERF_TARGET_H
38c2ecf20Sopenharmony_ci#define _PERF_TARGET_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <stdbool.h>
68c2ecf20Sopenharmony_ci#include <sys/types.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_cistruct target {
98c2ecf20Sopenharmony_ci	const char   *pid;
108c2ecf20Sopenharmony_ci	const char   *tid;
118c2ecf20Sopenharmony_ci	const char   *cpu_list;
128c2ecf20Sopenharmony_ci	const char   *uid_str;
138c2ecf20Sopenharmony_ci	uid_t	     uid;
148c2ecf20Sopenharmony_ci	bool	     system_wide;
158c2ecf20Sopenharmony_ci	bool	     uses_mmap;
168c2ecf20Sopenharmony_ci	bool	     default_per_cpu;
178c2ecf20Sopenharmony_ci	bool	     per_thread;
188c2ecf20Sopenharmony_ci};
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cienum target_errno {
218c2ecf20Sopenharmony_ci	TARGET_ERRNO__SUCCESS		= 0,
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	/*
248c2ecf20Sopenharmony_ci	 * Choose an arbitrary negative big number not to clash with standard
258c2ecf20Sopenharmony_ci	 * errno since SUS requires the errno has distinct positive values.
268c2ecf20Sopenharmony_ci	 * See 'Issue 6' in the link below.
278c2ecf20Sopenharmony_ci	 *
288c2ecf20Sopenharmony_ci	 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
298c2ecf20Sopenharmony_ci	 */
308c2ecf20Sopenharmony_ci	__TARGET_ERRNO__START		= -10000,
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	/* for target__validate() */
338c2ecf20Sopenharmony_ci	TARGET_ERRNO__PID_OVERRIDE_CPU	= __TARGET_ERRNO__START,
348c2ecf20Sopenharmony_ci	TARGET_ERRNO__PID_OVERRIDE_UID,
358c2ecf20Sopenharmony_ci	TARGET_ERRNO__UID_OVERRIDE_CPU,
368c2ecf20Sopenharmony_ci	TARGET_ERRNO__PID_OVERRIDE_SYSTEM,
378c2ecf20Sopenharmony_ci	TARGET_ERRNO__UID_OVERRIDE_SYSTEM,
388c2ecf20Sopenharmony_ci	TARGET_ERRNO__SYSTEM_OVERRIDE_THREAD,
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	/* for target__parse_uid() */
418c2ecf20Sopenharmony_ci	TARGET_ERRNO__INVALID_UID,
428c2ecf20Sopenharmony_ci	TARGET_ERRNO__USER_NOT_FOUND,
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	__TARGET_ERRNO__END,
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cienum target_errno target__validate(struct target *target);
488c2ecf20Sopenharmony_cienum target_errno target__parse_uid(struct target *target);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ciint target__strerror(struct target *target, int errnum, char *buf, size_t buflen);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic inline bool target__has_task(struct target *target)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	return target->tid || target->pid || target->uid_str;
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic inline bool target__has_cpu(struct target *target)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	return target->system_wide || target->cpu_list;
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic inline bool target__none(struct target *target)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	return !target__has_task(target) && !target__has_cpu(target);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic inline bool target__has_per_thread(struct target *target)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	return target->system_wide && target->per_thread;
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic inline bool target__uses_dummy_map(struct target *target)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	bool use_dummy = false;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	if (target->default_per_cpu)
778c2ecf20Sopenharmony_ci		use_dummy = target->per_thread ? true : false;
788c2ecf20Sopenharmony_ci	else if (target__has_task(target) ||
798c2ecf20Sopenharmony_ci	         (!target__has_cpu(target) && !target->uses_mmap))
808c2ecf20Sopenharmony_ci		use_dummy = true;
818c2ecf20Sopenharmony_ci	else if (target__has_per_thread(target))
828c2ecf20Sopenharmony_ci		use_dummy = true;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	return use_dummy;
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci#endif /* _PERF_TARGET_H */
88