PageRenderTime 90ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Linux/Smaps/Tiny.pm

https://github.com/avar/linux-smaps-tiny
Perl | 112 lines | 96 code | 16 blank | 0 comment | 0 complexity | 7d801bc4ec5f653efca1a629f38e8c81 MD5 | raw file
  1. package Linux::Smaps::Tiny;
  2. use strict;
  3. use warnings FATAL => "all";
  4. BEGIN {
  5. local ($@, $!);
  6. eval {
  7. require XSLoader;
  8. XSLoader::load(__PACKAGE__, $Linux::Smaps::Tiny::VERSION);
  9. 1;
  10. } or do {
  11. my $error = $@ // "Zombie Error";
  12. warn "Unable to load the XS version of <" . __PACKAGE__ . ">. Falling back on the pure-perl version!: <$@>";
  13. };
  14. }
  15. use Exporter 'import';
  16. our @EXPORT_OK = qw(get_smaps_summary);
  17. =encoding utf8
  18. =head1 NAME
  19. Linux::Smaps::Tiny - A minimal and fast alternative to L<Linux::Smaps>
  20. =head1 SYNOPSIS
  21. use Linux::Smaps::Tiny qw(get_smaps_summary);
  22. my $summary = get_smaps_summary();
  23. my $size = $summary->{Size};
  24. my $shared_clean = $summary->{Shared_Clean};
  25. my $shared_dirty = $summary->{Shared_Dirty};
  26. print "Size / Clean / Dirty = $size / $shared_clean / $shared_dirty\n";
  27. =head1 DESCRIPTION
  28. This module is a tiny interface to F</proc/PID/smaps> files. It was
  29. written because when we rolled out L<Linux::Smaps> in some critical
  30. code at a Big Internet Company we experienced slowdowns that were
  31. solved by writing a more minimal version.
  32. This module will try to use XS code to parse the smaps file, and if
  33. that doesn't work it'll fall back on a pure-Perl version. You can also
  34. use only the pure-perl version by using L<Linux::Smaps::Tiny::PP>
  35. directly, it has the same API and exports.
  36. We'll warn on compile-time if we can't load the XS version.
  37. If speed isn't a concern you should probably use L<Linux::Smaps>
  38. instead. Also note that L<Linux::Smaps> itself L<has been
  39. optimized|http://mail-archives.apache.org/mod_mbox/perl-modperl/201103.mbox/browser>
  40. since this module was initially written.
  41. =head2 SPEED
  42. The distribution comes with a F<contrib/benchmark.pl> script. As of
  43. writing this is the speed of L<Linux::Smaps> 0.13
  44. v.s. L<Linux::Smaps::Tiny 0.11>, both the XS and PP versions:
  45. Rate Linux::Smaps Linux::Smaps::Tiny::PP Linux::Smaps::Tiny
  46. Linux::Smaps 672/s -- -37% -74%
  47. Linux::Smaps::Tiny::PP 1067/s 59% -- -59%
  48. Linux::Smaps::Tiny 2618/s 290% 145% --
  49. =head1 FUNCTIONS
  50. =head2 get_smaps_summary
  51. Takes an optional process id (defaults to C<self>) returns a summary
  52. of the smaps data for the given process. Dies if the process does not
  53. exist.
  54. Returns a hashref like this:
  55. {
  56. 'MMUPageSize' => '184',
  57. 'Private_Clean' => '976',
  58. 'Swap' => '0',
  59. 'KernelPageSize' => '184',
  60. 'Pss' => '1755',
  61. 'Private_Dirty' => '772',
  62. 'Referenced' => '2492',
  63. 'Size' => '5456',
  64. 'Shared_Clean' => '744',
  65. 'Shared_Dirty' => '0',
  66. 'Rss' => '2492'
  67. };
  68. Values are in kB.
  69. =cut
  70. unless (defined &get_smaps_summary) {
  71. require Linux::Smaps::Tiny::PP;
  72. *get_smaps_summary = \&Linux::Smaps::Tiny::PP::get_smaps_summary;
  73. }
  74. =head1 LICENSE AND COPYRIGHT
  75. Copyright 2011-2016 Yves Orton <yves@cpan.org> and Ævar Arnfjörð
  76. Bjarmason <avar@cpan.org>
  77. This program is free software, you can redistribute it and/or modify
  78. it under the same terms as Perl itself.
  79. =cut
  80. 1;