Skip to content

Instantly share code, notes, and snippets.

@bahamas10
Last active Aug 29, 2015
Embed
What would you like to do?
pfiles in zone
#include <fcntl.h>
#include <stdio.h>
#define FILE_TO_OPEN "/etc/passwd"
#define TIMES 5000
int main() {
int fd;
int i;
printf("pid: %d\n", getpid());
for (i = 0; i < TIMES; i++) {
if (fd = open(FILE_TO_OPEN, O_RDONLY) == -1) {
perror("open");
return 1;
}
}
sleep(300);
return 0;
}
dave - sjc1-dave-01 sunos ~ $ ./open-5k &
[1] 32690
pid: 32690
dave - sjc1-dave-01 sunos ~ $ sudo dtrace -n 'syscall:::entry /pid == $target/ { @[probefunc]=count(); }' -c "sudo pfiles 32690"
dtrace: description 'syscall:::entry ' matched 233 probes
...
(pfiles output)
forksys 1
getcwd 1
pipe 1
putmsg 1
recv 1
schedctl 1
setrlimit 1
sigpending 1
so_socketpair 1
sysi86 1
waitsys 1
write 1
getrlimit 2
gtime 2
munmap 2
privsys 2
setpgrp 2
systeminfo 2
lwp_sigmask 3
pollsys 3
setcontext 3
so_socket 3
getgroups 4
setgroups 4
setuid 4
sysconfig 4
setregid 8
setreuid 8
getpid 9
fcntl 10
lseek 10
priocntlsys 11
ioctl 12
brk 15
doorfs 18
getuid 20
fstat 23
sigaction 33
memcntl 36
mmapobj 39
resolvepath 39
lwp_cond_broadcast 40
read 43
mmap 51
close 62
open 65
stat 87
#!/usr/bin/env node
var net = require('net');
for (var i = 0; i < 5000; i++)
net.connect(80, 'daveeddy.com');
dave - sjc1-dave-01 sunos ~ $ ./open-5k-sockets.js &
[1] 34728
dave - sjc1-dave-01 sunos ~ $ sudo ptime pfiles 34728
34728: node ./open-5k-sockets.js
real 1:28.918439782
user 0.695646071
sys 1:26.230105158
@bahamas10

This comment has been minimized.

Copy link
Owner Author

@bahamas10 bahamas10 commented Dec 4, 2014

dtracing open-5k-sockets pfiles (without -n)

sudo dtrace -n 'syscall:::entry /pid == $target/ {self->ts = timestamp} syscall:::return /self->ts/ { @[probefunc] = sum(timestamp - self->ts); self->ts = 0}' -c 'pfiles 37433'
...
  sigpending                                                     2922
  privsys                                                        3514
  fstat64                                                        4933
  ioctl                                                          5659
  zone                                                           5931
  llseek                                                         7320
  getpid                                                         9821
  sysconfig                                                     11425
  lseek                                                         11640
  unlink                                                        11968
  access                                                        12263
  openat                                                        12730
  getrlimit                                                     12774
  systeminfo                                                    13234
  lstat                                                         14326
  sigaction                                                     15174
  setcontext                                                    16213
  sysi86                                                        22980
  schedctl                                                      32911
  lwp_cond_broadcast                                            46024
  fstat                                                         86103
  close                                                        126927
  stat                                                         149194
  resolvepath                                                  149460
  mmap                                                         151962
  getdents                                                     328351
  open                                                         331440
  fcntl                                                        333310
  memcntl                                                      516752
  mmapobj                                                      725602
  read                                                         956326
  exece                                                       1053581
  brk                                                         2934606
  lwp_sigmask                                               278688725
  writev                                                    814213280
  pwrite                                                   1100104380
  write                                                    2514767301
  pread                                                    6120029001
  readlink                                               120421775540
@bahamas10

This comment has been minimized.

Copy link
Owner Author

@bahamas10 bahamas10 commented Dec 5, 2014

  8  14722                   readlink:entry 
              libc.so.1`__systemcall6+0x1e
              libproc.so.1`load_fdinfo+0x17c
              libproc.so.1`Pfdinfo_iter+0x32
              pfiles`show_files+0x4b
              pfiles`main+0x1b6
              pfiles`_start+0x6c
@bahamas10

This comment has been minimized.

@bahamas10

This comment has been minimized.

Copy link
Owner Author

@bahamas10 bahamas10 commented Dec 5, 2014

Cut 1k open files down to 4 seconds by LD_PRELOADing a readlink that does nothing

#include <stdlib.h>
#include <errno.h>
size_t readlink(const char *path, char *buf, size_t bufsize) {
        errno = ENOSYS;
        return -1;
}
$ cc -shared -fPIC -o readlink.so readlink.c
$ node open-1k-sockets.js &
[1] 88758
$ LD_PRELOAD=./readlink.so /usr/bin/amd64/ptime /usr/bin/amd64/pfiles 88758
... 1000 open sockets ...

real        4.061913621
user        0.354056032
sys         2.692370925
@bahamas10

This comment has been minimized.

Copy link
Owner Author

@bahamas10 bahamas10 commented Dec 5, 2014

diff --git a/usr/src/lib/libproc/common/Pfdinfo.c b/usr/src/lib/libproc/common/Pfdinfo.c
index 1244397..b5e4d83 100644
--- a/usr/src/lib/libproc/common/Pfdinfo.c
+++ b/usr/src/lib/libproc/common/Pfdinfo.c
@@ -134,10 +134,17 @@ load_fdinfo(struct ps_prochandle *P)
                        info->pr_offset = pr_llseek(P, fd, 0, SEEK_CUR);

                        /* attempt to determine the path to it */
-                       (void) snprintf(path, sizeof (path),
-                           "%s/%d/path/%d", procfs_path, (int)P->pid, fd);
-                       len = readlink(path, info->pr_path,
-                           sizeof (info->pr_path) - 1);
+                       switch (info->pr_mode & S_IFMT) {
+                       case S_IFSOCK: case S_IFDOOR: /* not applicable */
+                               len = -1;
+                               break;
+                       default:
+                               (void) snprintf(path, sizeof (path),
+                                   "%s/%d/path/%d", procfs_path, (int)P->pid, fd);
+                               len = readlink(path, info->pr_path,
+                                   sizeof (info->pr_path) - 1);
+                               break;
+                       }

                        if (len < 0) {
                                info->pr_path[0] = 0;
@bahamas10

This comment has been minimized.

Copy link
Owner Author

@bahamas10 bahamas10 commented Dec 5, 2014

without LD_PRELOAD

dave - sjc1-dave-01 sunos ~/pfiles $ ./open-sockets.js &
[1] 75375
dave - sjc1-dave-01 sunos ~/pfiles $ opened 1000 sockets
ptime pfiles 75375 > /dev/null

real       45.522214487
user        0.334410765
sys        44.691393983

with LD_PRELOAD

dave - sjc1-dave-01 sunos ~/pfiles $ ./open-sockets.js &
[1] 75690
dave - sjc1-dave-01 sunos ~/pfiles $ opened 1000 sockets
LD_PRELOAD=./readlink.so /usr/bin/amd64/ptime /usr/bin/amd64/pfiles 75690 > /dev/null

real        3.887839734
user        0.328562456
sys         2.558752763
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment