xref: /titanic_41/usr/src/cmd/sgs/link_audit/common/perfcnt.c (revision 6a634c9dca3093f3922e4b7ab826d7bdf17bf78e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*20c1c355SRod Evans  * Common Development and Distribution License (the "License").
6*20c1c355SRod Evans  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*20c1c355SRod Evans 
227c478bd9Sstevel@tonic-gate /*
23*20c1c355SRod Evans  * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate #include <errno.h>
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <unistd.h>
297c478bd9Sstevel@tonic-gate #include <stropts.h>
307c478bd9Sstevel@tonic-gate #include <link.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/regset.h>
337c478bd9Sstevel@tonic-gate #include <sys/frame.h>
347c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
357c478bd9Sstevel@tonic-gate #include <fcntl.h>
367c478bd9Sstevel@tonic-gate #include <signal.h>
377c478bd9Sstevel@tonic-gate #include "env.h"
387c478bd9Sstevel@tonic-gate #include "hash.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate typedef struct {
427c478bd9Sstevel@tonic-gate 	float		d_time;
437c478bd9Sstevel@tonic-gate 	int		d_count;
447c478bd9Sstevel@tonic-gate 	const char	*d_symname;
457c478bd9Sstevel@tonic-gate } d_entry;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate typedef struct list {
487c478bd9Sstevel@tonic-gate 	d_entry		*l_dep;
497c478bd9Sstevel@tonic-gate 	struct list	*l_next;
507c478bd9Sstevel@tonic-gate } List;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 
53*20c1c355SRod Evans static Elist	    *bindto_list = NULL;
54*20c1c355SRod Evans static Elist	    *bindfrom_list = NULL;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static int  initialized;
577c478bd9Sstevel@tonic-gate extern long long gethrvtime();
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static const char *progname;
607c478bd9Sstevel@tonic-gate static long long starts[1000];
617c478bd9Sstevel@tonic-gate static long long accounted[1000];		/* time accounted for */
627c478bd9Sstevel@tonic-gate static int  counter = 0;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static float	total_time = 0.0;
65*20c1c355SRod Evans static List	*list_head = NULL;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static hash	*tbl;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate static sigset_t		iset;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static void
list_insert(d_entry * dep)727c478bd9Sstevel@tonic-gate list_insert(d_entry *dep)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	List *new_list;
757c478bd9Sstevel@tonic-gate 	List *cur;
767c478bd9Sstevel@tonic-gate 	List *prev;
777c478bd9Sstevel@tonic-gate 
78*20c1c355SRod Evans 	if ((new_list = malloc(sizeof (List))) == NULL) {
797c478bd9Sstevel@tonic-gate 		(void) printf("libperfcnt.so: malloc failed - "
807c478bd9Sstevel@tonic-gate 		    "can't print summary\n");
817c478bd9Sstevel@tonic-gate 		exit(1);
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 	new_list->l_dep = dep;
847c478bd9Sstevel@tonic-gate 
85*20c1c355SRod Evans 	if (list_head == NULL) {
867c478bd9Sstevel@tonic-gate 		list_head = new_list;
87*20c1c355SRod Evans 		new_list->l_next = NULL;
887c478bd9Sstevel@tonic-gate 		return;
897c478bd9Sstevel@tonic-gate 	}
90*20c1c355SRod Evans 	for (cur = list_head, prev = NULL;
917c478bd9Sstevel@tonic-gate 	    (cur && (cur->l_dep->d_time < dep->d_time));
927c478bd9Sstevel@tonic-gate 	    prev = cur, cur = cur->l_next)
937c478bd9Sstevel@tonic-gate 		;
947c478bd9Sstevel@tonic-gate 	/*
957c478bd9Sstevel@tonic-gate 	 * insert at head of list
967c478bd9Sstevel@tonic-gate 	 */
97*20c1c355SRod Evans 	if (prev == NULL) {
987c478bd9Sstevel@tonic-gate 		new_list->l_next = list_head;
997c478bd9Sstevel@tonic-gate 		list_head = new_list;
1007c478bd9Sstevel@tonic-gate 		return;
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 	prev->l_next = new_list;
1037c478bd9Sstevel@tonic-gate 	new_list->l_next = cur;
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate uint_t
la_version(uint_t version)1077c478bd9Sstevel@tonic-gate la_version(uint_t version)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	int fd;
1107c478bd9Sstevel@tonic-gate 	char buffer[100];
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if (version > LAV_CURRENT)
1137c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "perfcnt.so.1: unexpected version: %d\n",
1147c478bd9Sstevel@tonic-gate 		    version);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	(void) sprintf(buffer, "/proc/%d", (int)getpid());
1177c478bd9Sstevel@tonic-gate 	if ((fd = open(buffer, O_RDWR)) >= 0) {
1187c478bd9Sstevel@tonic-gate 		long state = PR_MSACCT;
1197c478bd9Sstevel@tonic-gate 		if (ioctl(fd, PIOCSET, &state) == -1)
1207c478bd9Sstevel@tonic-gate 			perror("PIOCSET");
1217c478bd9Sstevel@tonic-gate 		(void) close(fd);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	initialized++;
1257c478bd9Sstevel@tonic-gate 	tbl = make_hash(213);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	build_env_list(&bindto_list, (const char *)"PERFCNT_BINDTO");
1287c478bd9Sstevel@tonic-gate 	build_env_list(&bindto_list, (const char *)"PERFCNT_BINDFROM");
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/*
1317c478bd9Sstevel@tonic-gate 	 * Initalize iset to the full set of signals to be masked durring
1327c478bd9Sstevel@tonic-gate 	 * pltenter/pltexit
1337c478bd9Sstevel@tonic-gate 	 */
1347c478bd9Sstevel@tonic-gate 	(void) sigfillset(&iset);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	return (LAV_CURRENT);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
1417c478bd9Sstevel@tonic-gate uint_t
la_objopen(Link_map * lmp,Lmid_t lmid,uintptr_t * cookie)1427c478bd9Sstevel@tonic-gate la_objopen(Link_map *lmp, Lmid_t lmid, uintptr_t *cookie)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	static int	first = 1;
1457c478bd9Sstevel@tonic-gate 	uint_t	flags = 0;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if (first) {
1487c478bd9Sstevel@tonic-gate 		progname = lmp->l_name;
1497c478bd9Sstevel@tonic-gate 		first = 0;
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
152*20c1c355SRod Evans 	if (bindto_list == NULL)
1537c478bd9Sstevel@tonic-gate 		flags = LA_FLG_BINDTO;
1547c478bd9Sstevel@tonic-gate 	else {
1557c478bd9Sstevel@tonic-gate 		if (check_list(bindto_list, lmp->l_name))
1567c478bd9Sstevel@tonic-gate 			flags = LA_FLG_BINDTO;
1577c478bd9Sstevel@tonic-gate 	}
158*20c1c355SRod Evans 	if (bindfrom_list == NULL)
1597c478bd9Sstevel@tonic-gate 		flags |= LA_FLG_BINDFROM;
1607c478bd9Sstevel@tonic-gate 	else {
1617c478bd9Sstevel@tonic-gate 		if (check_list(bindfrom_list, lmp->l_name))
1627c478bd9Sstevel@tonic-gate 			flags |= LA_FLG_BINDFROM;
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	return (flags);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
1697c478bd9Sstevel@tonic-gate #if	defined(__sparcv9)
1707c478bd9Sstevel@tonic-gate uintptr_t
la_sparcv9_pltenter(Elf64_Sym * symp,uint_t symndx,uintptr_t * refcookie,uintptr_t * defcookie,La_sparcv9_regs * regset,uint_t * sb_flags,const char * sym_name)1717c478bd9Sstevel@tonic-gate la_sparcv9_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
1727c478bd9Sstevel@tonic-gate 	uintptr_t *defcookie, La_sparcv9_regs *regset, uint_t *sb_flags,
1737c478bd9Sstevel@tonic-gate 	const char *sym_name)
1747c478bd9Sstevel@tonic-gate #elif	defined(__sparc)
1757c478bd9Sstevel@tonic-gate uintptr_t
1767c478bd9Sstevel@tonic-gate la_sparcv8_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
1777c478bd9Sstevel@tonic-gate 	uintptr_t *defcookie, La_sparcv8_regs *regset, uint_t *sb_flags)
1787c478bd9Sstevel@tonic-gate #elif	defined(__amd64)
1797c478bd9Sstevel@tonic-gate uintptr_t
1807c478bd9Sstevel@tonic-gate la_amd64_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
1817c478bd9Sstevel@tonic-gate 	uintptr_t *defcookie, La_amd64_regs *regset, uint_t *sb_flags,
1827c478bd9Sstevel@tonic-gate 	const char *sym_name)
1837c478bd9Sstevel@tonic-gate #elif	defined(__i386)
1847c478bd9Sstevel@tonic-gate uintptr_t
1857c478bd9Sstevel@tonic-gate la_i86_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcooke,
1867c478bd9Sstevel@tonic-gate 	uintptr_t *defcook, La_i86_regs *regset, uint_t *sb_flags)
1877c478bd9Sstevel@tonic-gate #endif
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	accounted[counter] = 0;
1907c478bd9Sstevel@tonic-gate 	starts[counter] = gethrvtime();
1917c478bd9Sstevel@tonic-gate 	counter++;
1927c478bd9Sstevel@tonic-gate 	return (symp->st_value);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
1987c478bd9Sstevel@tonic-gate #if	defined(_LP64)
1997c478bd9Sstevel@tonic-gate /* ARGSUSED */
2007c478bd9Sstevel@tonic-gate uintptr_t
la_pltexit64(Elf64_Sym * symp,uint_t symndx,uintptr_t * refcookie,uintptr_t * defcookie,uintptr_t retval,const char * sym_name)2017c478bd9Sstevel@tonic-gate la_pltexit64(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
2027c478bd9Sstevel@tonic-gate 	uintptr_t *defcookie, uintptr_t retval, const char *sym_name)
2037c478bd9Sstevel@tonic-gate #else
2047c478bd9Sstevel@tonic-gate uintptr_t
2057c478bd9Sstevel@tonic-gate la_pltexit(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
2067c478bd9Sstevel@tonic-gate 	uintptr_t *defcookie, uintptr_t retval)
2077c478bd9Sstevel@tonic-gate #endif
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	d_entry		**dep;
2107c478bd9Sstevel@tonic-gate 	long long	time_used;
2117c478bd9Sstevel@tonic-gate 	sigset_t	oset;
2127c478bd9Sstevel@tonic-gate #if	!defined(_LP64)
2137c478bd9Sstevel@tonic-gate 	const char	*sym_name = (const char *)symp->st_name;
2147c478bd9Sstevel@tonic-gate #endif
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &iset, &oset);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	counter--;
2197c478bd9Sstevel@tonic-gate 	time_used = gethrvtime() - starts[counter];
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	dep = (d_entry **)get_hash(tbl, (char *)sym_name);
2227c478bd9Sstevel@tonic-gate 	if (*dep == NULL) {
2237c478bd9Sstevel@tonic-gate 		char *ptr = malloc(sizeof (d_entry));
2247c478bd9Sstevel@tonic-gate 		/* LINTED */
2257c478bd9Sstevel@tonic-gate 		(*dep) = (d_entry *)ptr;
2267c478bd9Sstevel@tonic-gate 		(*dep)->d_count = 0;
2277c478bd9Sstevel@tonic-gate 		(*dep)->d_time = 0.0;
2287c478bd9Sstevel@tonic-gate 		(*dep)->d_symname = sym_name;
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	if (counter)
2327c478bd9Sstevel@tonic-gate 		accounted[counter - 1] += time_used;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	((*dep)->d_count)++;
2357c478bd9Sstevel@tonic-gate 	(*dep)->d_time += (double)((time_used - accounted[counter]) / 1.0e9);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	return (retval);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
2437c478bd9Sstevel@tonic-gate static void
scanlist(d_entry * dep,void * food,char * name)2447c478bd9Sstevel@tonic-gate scanlist(d_entry *dep, void *food, char *name)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	total_time += dep->d_time;
2477c478bd9Sstevel@tonic-gate 	list_insert(dep);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate #pragma fini(cleanup)
2517c478bd9Sstevel@tonic-gate static void
cleanup()2527c478bd9Sstevel@tonic-gate cleanup()
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	List	*cur;
2557c478bd9Sstevel@tonic-gate 	(void) operate_hash(tbl, scanlist, NULL);
2567c478bd9Sstevel@tonic-gate 	(void) printf("\n\nPerf Counts for: %s\n\n", progname);
2577c478bd9Sstevel@tonic-gate 	(void) printf("%20s\tc_count\t    tim\t\tavg. tim\ttot. %%\n",
2587c478bd9Sstevel@tonic-gate 	    "SYMBOL");
2597c478bd9Sstevel@tonic-gate 	(void) printf("--------------------------------------------------"
2607c478bd9Sstevel@tonic-gate 	    "-------------------\n");
2617c478bd9Sstevel@tonic-gate 	for (cur = list_head; cur; cur = cur->l_next) {
2627c478bd9Sstevel@tonic-gate 		d_entry		*dep = cur->l_dep;
2637c478bd9Sstevel@tonic-gate 		float		tim = dep->d_time * 1000000;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 		(void) printf("%20s\t%d\t%8.2f\t%8.2f\t%2.2f%%\n",
2667c478bd9Sstevel@tonic-gate 		    dep->d_symname, dep->d_count, tim, tim / dep->d_count,
2677c478bd9Sstevel@tonic-gate 		    ((dep->d_time / total_time) * 100.0));
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 	(void) printf("--------------------------------------------------"
2707c478bd9Sstevel@tonic-gate 	    "-------------------\n");
2717c478bd9Sstevel@tonic-gate 	(void) printf("\t\t\t\t\t\tTotal Time: %8.2f\n",
2727c478bd9Sstevel@tonic-gate 	    total_time * 1000000);
2737c478bd9Sstevel@tonic-gate }
274