test_linux_reuseport_tcp_listen_stats: skip w/o unicorn
[raindrops.git] / lib / raindrops.rb
blobdc61952ffd82c3e42ad34574c6f46f57d225abdf
1 # -*- encoding: binary -*-
3 # Each Raindrops object is a container that holds several counters.
4 # It is internally a page-aligned, shared memory area that allows
5 # atomic increments, decrements, assignments and reads without any
6 # locking.
8 #   rd = Raindrops.new 4
9 #   rd.incr(0, 1)   -> 1
10 #   rd.to_ary       -> [ 1, 0, 0, 0 ]
12 # Unlike many classes in this package, the core Raindrops class is
13 # intended to be portable to all reasonably modern *nix systems
14 # supporting mmap().  Please let us know if you have portability
15 # issues, patches or pull requests at mailto:raindrops-public@yhbt.net
16 class Raindrops
18   # Used to represent the number of +active+ and +queued+ sockets for
19   # a single listen socket across all threads and processes on a
20   # machine.
21   #
22   # For TCP listeners, only sockets in the TCP_ESTABLISHED state are
23   # accounted for.  For Unix domain listeners, only CONNECTING and
24   # CONNECTED Unix domain sockets are accounted for.
25   #
26   # +active+ connections is the number of accept()-ed but not-yet-closed
27   # sockets in all threads/processes sharing the given listener.
28   #
29   # +queued+ connections is the number of un-accept()-ed sockets in the
30   # queue of a given listen socket.
31   #
32   # These stats are currently only available under \Linux
33   class ListenStats < Struct.new(:active, :queued)
35     # the sum of +active+ and +queued+ sockets
36     def total
37       active + queued
38     end
39   end unless defined? ListenStats
41   # call-seq:
42   #     Raindrops.new(size, io: nil)    -> raindrops object
43   #
44   # Initializes a Raindrops object to hold +size+ counters.  +size+ is
45   # only a hint and the actual number of counters the object has is
46   # dependent on the CPU model, number of cores, and page size of
47   # the machine.  The actual size of the object will always be equal
48   # or greater than the specified +size+.
49   # If +io+ is provided, then the Raindrops memory will be backed by
50   # the specified file; otherwise, it will allocate anonymous memory.
51   # The IO object must respond to +truncate+, as this is used to set
52   # the size of the file.
53   # If +zero+ is provided, then the memory region is zeroed prior to
54   # returning. This is only meaningful if +io+ is also provided; in
55   # that case it controls whether any existing counter values in +io+
56   # are retained (false) or whether it is entirely zeroed (true).
57   def initialize(size, io: nil, zero: false)
58     # This ruby wrapper exists to handle the keyword-argument handling,
59     # which is otherwise kind of awkward in C. We delegate the keyword
60     # arguments to the _actual_ initialize implementation as positional
61     # args.
62     initialize_cimpl(size, io, zero)
63   end
65   autoload :Linux, 'raindrops/linux'
66   autoload :Struct, 'raindrops/struct'
67   autoload :Middleware, 'raindrops/middleware'
68   autoload :Aggregate, 'raindrops/aggregate'
69   autoload :LastDataRecv, 'raindrops/last_data_recv'
70   autoload :Watcher, 'raindrops/watcher'
71 end
72 require 'raindrops_ext'