BSD libc / "Get statistics of function calling"

  1. Introduction
  2. Function frequency count script
  3. string
  4. continue...

Last updated 2001-07-30 5:37 pm


How many times each function is called?

There are so many functions in BSDlibc, and first I treid to classify them according to the number of call in ./src. Here is a simple AWK script for the purpose, but I'm afraid that it is incomplete and inefficient.

An AWK script: "scan"
     
#! /usr/bin/awk -f ### Feb.5,2001 Count frequency of libc functions BEGIN { file="lists" while ((getline tmp < file) > 0) { if (tmp == "") continue if (index(tmp, "#") == 1) continue functions[ tmp ] = 0 } close(file) } { for (i in functions) { key = "[^_a-z0-9]" i "\(" if (match($0, key) > 0) functions[i]++ } } END { for (i in functions) { printf("%16s : %d\n", i, functions[i]) } }

Prepare a function list

BSDlibc is composed of many subdirectories, then let's start from "string"! There are many familiar functions in this directory, and almost of them are independent of other library functions. The "scan" script reads function name lists delimited by new line from a file "lists". The file may contain comment line starting with # or blank lines. Here is a list of "string" functions. They are divided into 4 categories, string, bstring, bm, and others.

lists
     
### string ### strcat strlcat strncat strchr strrchr strcmp strncmp strcasecmp strncasecmp strcpy strlcpy strncpy strerror strlen strpbrk strsep strspn strcspn strstr strtok index rindex ### bstring ### bcmp bcopy bzero memccpy memchr memcmp memcpy memmove memset ### bm ### bm_comp bm_exec bm_free ### others ### ffs strcoll strdup strmode strsignal strxfrm swab

Execute the "scan"

Finally, execute the script as follows. It took about 45 minutes in my Celeron/500MHz machine.

     
$ cd /usr/src/openbsd $ find src/ -name *.c -exec cat {} ";" | ./scan - >lst $ cat lst strlcat : 156 strsep : 150 bzero : 2330 index : 92 memchr : 45 strdup : 772 strcat : 999 strncasecmp : 342 memmove : 406 strncpy : 1257 strcspn : 43 strlen : 6483 rindex : 5 memcmp : 455 bm_free : 1 ffs : 90 strcasecmp : 960 strmode : 11 strncat : 104 strpbrk : 117 strchr : 2189 strspn : 64 strerror : 1303 strtok : 248 memccpy : 1 bcmp : 394 strcoll : 8 memcpy : 2377 strcmp : 5289 strrchr : 587 bm_exec : 0 strlcpy : 774 memset : 2302 strstr : 311 strxfrm : 5 bcopy : 2838 bm_comp : 0 strsignal : 12 strncmp : 1837 strcpy : 2883 swab : 1

Now, we got the list!

The above list is somehow unclear. Then, sort the list by frequency.

     
$ cat lst | sort -nr +.19 strlen : 6483 strcmp : 5289 strcpy : 2883 bcopy : 2838 memcpy : 2377 bzero : 2330 memset : 2302 strchr : 2189 strncmp : 1837 strerror : 1303 strncpy : 1257 strcat : 999 strcasecmp : 960 strlcpy : 774 strdup : 772 strrchr : 587 memcmp : 455 memmove : 406 bcmp : 394 strncasecmp : 342 strstr : 311 strtok : 248 strlcat : 156 strsep : 150 strpbrk : 117 strncat : 104 index : 92 ffs : 90 strspn : 64 memchr : 45 strcspn : 43 strsignal : 12 strmode : 11 strcoll : 8 strxfrm : 5 rindex : 5 memccpy : 1 bm_free : 1 swab : 1 bm_exec : 0 bm_comp : 0

Well, I will use this shapeless script for a while.