Ejemplo n.º 1
0
def _get_env_value_ms(key, default_s):
    try:
        val = int(os.getenv(key, default_s * 1000)) / 1000
    except:
        val = -1

    if val < 0:
        fmt = 'invalid ${} value; {} seconds will be used'
        dbg._pwarning(fmt.format(key, default_s))
        val = default_s

    return val
Ejemplo n.º 2
0
def _get_env_value_ms(key, default_s):
    try:
        val = int(os.getenv(key, default_s * 1000)) / 1000
    except:
        val = -1

    if val < 0:
        fmt = "invalid ${} value; {} seconds will be used"
        dbg._pwarning(fmt.format(key, default_s))
        val = default_s

    return val
Ejemplo n.º 3
0
    def __init__(self, name, host, port, reg_queue):
        super(self.__class__, self).__init__()
        self._name = name
        self._host = host
        self._port = port

        try:
            self._log_handler = lttngust.loghandler._Handler()
        except (OSError) as e:
            dbg._pwarning("cannot load library: {}".format(e))
            raise e

        self._root_logger = logging.getLogger()
        self._root_logger.setLevel(logging.NOTSET)
        self._ref_count = 0
        self._sessiond_sock = None
        self._reg_queue = reg_queue
        self._server_cmd_handlers = {
            lttngust.cmd._ServerCmdRegistrationDone: self._handle_server_cmd_reg_done,
            lttngust.cmd._ServerCmdEnable: self._handle_server_cmd_enable,
            lttngust.cmd._ServerCmdDisable: self._handle_server_cmd_disable,
            lttngust.cmd._ServerCmdList: self._handle_server_cmd_list,
        }
Ejemplo n.º 4
0
    def __init__(self, name, host, port, reg_queue):
        super(self.__class__, self).__init__()
        self._name = name
        self._host = host
        self._port = port

        try:
            self._log_handler = lttngust.loghandler._Handler()
        except (OSError) as e:
            dbg._pwarning('cannot load library: {}'.format(e))
            raise e

        self._root_logger = logging.getLogger()
        self._root_logger.setLevel(logging.NOTSET)
        self._ref_count = 0
        self._sessiond_sock = None
        self._reg_queue = reg_queue
        self._server_cmd_handlers = {
            lttngust.cmd._ServerCmdRegistrationDone:
            self._handle_server_cmd_reg_done,
            lttngust.cmd._ServerCmdEnable: self._handle_server_cmd_enable,
            lttngust.cmd._ServerCmdDisable: self._handle_server_cmd_disable,
            lttngust.cmd._ServerCmdList: self._handle_server_cmd_list,
        }
Ejemplo n.º 5
0
def _init_threads():
    global _initialized

    dbg._pdebug('entering')

    if _initialized:
        dbg._pdebug('agent is already initialized')
        return

    # This makes sure that the appropriate modules for encoding and
    # decoding strings/bytes are imported now, since no import should
    # happen within a thread at import time (our case).
    'lttng'.encode().decode()

    _initialized = True
    sys_port = _get_port_from_file('/var/run/lttng/agent.port')
    user_port_file = os.path.join(_get_user_home_path(), '.lttng',
                                  'agent.port')
    user_port = _get_port_from_file(user_port_file)
    reg_queue = queue.Queue()
    reg_expecting = 0

    dbg._pdebug('system session daemon port: {}'.format(sys_port))
    dbg._pdebug('user session daemon port: {}'.format(user_port))

    if sys_port == user_port and sys_port is not None:
        # The two session daemon ports are the same. This is not normal.
        # Connect to only one.
        dbg._pdebug('both user and system session daemon have the same port')
        sys_port = None

    try:
        if sys_port is not None:
            dbg._pdebug('creating system client thread')
            t = threading.Thread(target=_client_thread_target,
                                 args=('system', sys_port, reg_queue))
            t.name = 'system'
            t.daemon = True
            t.start()
            dbg._pdebug('created and started system client thread')
            reg_expecting += 1

        if user_port is not None:
            dbg._pdebug('creating user client thread')
            t = threading.Thread(target=_client_thread_target,
                                 args=('user', user_port, reg_queue))
            t.name = 'user'
            t.daemon = True
            t.start()
            dbg._pdebug('created and started user client thread')
            reg_expecting += 1
    except:
        # cannot create threads for some reason; stop this initialization
        dbg._pwarning('cannot create client threads')
        return

    if reg_expecting == 0:
        # early exit: looks like there's not even one valid port
        dbg._pwarning(
            'no valid LTTng session daemon port found (is the session daemon started?)'
        )
        return

    cur_timeout = _REG_TIMEOUT

    # We block here to make sure the agent is properly registered to
    # the session daemon. If we timeout, the client threads will still
    # continue to try to connect and register to the session daemon,
    # but there is no guarantee that all following logging statements
    # will make it to LTTng-UST.
    #
    # When a client thread receives a "registration done" confirmation
    # from the session daemon it's connected to, it puts True in
    # reg_queue.
    while True:
        try:
            dbg._pdebug(
                'waiting for registration done (expecting {}, timeout is {} s)'
                .format(reg_expecting, cur_timeout))
            t1 = lttngust.compat._clock()
            reg_queue.get(timeout=cur_timeout)
            t2 = lttngust.compat._clock()
            reg_expecting -= 1
            dbg._pdebug('unblocked')

            if reg_expecting == 0:
                # done!
                dbg._pdebug('successfully registered to session daemon(s)')
                break

            cur_timeout -= (t2 - t1)

            if cur_timeout <= 0:
                # timeout
                dbg._pdebug('ran out of time')
                break
        except queue.Empty:
            dbg._pdebug('ran out of time')
            break

    dbg._pdebug('leaving')
Ejemplo n.º 6
0
def _init_threads():
    global _initialized

    dbg._pdebug("entering")

    if _initialized:
        dbg._pdebug("agent is already initialized")
        return

    # This makes sure that the appropriate modules for encoding and
    # decoding strings/bytes are imported now, since no import should
    # happen within a thread at import time (our case).
    "lttng".encode().decode()

    _initialized = True
    sys_port = _get_port_from_file("/var/run/lttng/agent.port")
    user_port_file = os.path.join(_get_user_home_path(), ".lttng", "agent.port")
    user_port = _get_port_from_file(user_port_file)
    reg_queue = queue.Queue()
    reg_expecting = 0

    dbg._pdebug("system session daemon port: {}".format(sys_port))
    dbg._pdebug("user session daemon port: {}".format(user_port))

    if sys_port == user_port and sys_port is not None:
        # The two session daemon ports are the same. This is not normal.
        # Connect to only one.
        dbg._pdebug("both user and system session daemon have the same port")
        sys_port = None

    try:
        if sys_port is not None:
            dbg._pdebug("creating system client thread")
            t = threading.Thread(target=_client_thread_target, args=("system", sys_port, reg_queue))
            t.name = "system"
            t.daemon = True
            t.start()
            dbg._pdebug("created and started system client thread")
            reg_expecting += 1

        if user_port is not None:
            dbg._pdebug("creating user client thread")
            t = threading.Thread(target=_client_thread_target, args=("user", user_port, reg_queue))
            t.name = "user"
            t.daemon = True
            t.start()
            dbg._pdebug("created and started user client thread")
            reg_expecting += 1
    except:
        # cannot create threads for some reason; stop this initialization
        dbg._pwarning("cannot create client threads")
        return

    if reg_expecting == 0:
        # early exit: looks like there's not even one valid port
        dbg._pwarning("no valid LTTng session daemon port found (is the session daemon started?)")
        return

    cur_timeout = _REG_TIMEOUT

    # We block here to make sure the agent is properly registered to
    # the session daemon. If we timeout, the client threads will still
    # continue to try to connect and register to the session daemon,
    # but there is no guarantee that all following logging statements
    # will make it to LTTng-UST.
    #
    # When a client thread receives a "registration done" confirmation
    # from the session daemon it's connected to, it puts True in
    # reg_queue.
    while True:
        try:
            dbg._pdebug(
                "waiting for registration done (expecting {}, timeout is {} s)".format(reg_expecting, cur_timeout)
            )
            t1 = time.clock()
            reg_queue.get(timeout=cur_timeout)
            t2 = time.clock()
            reg_expecting -= 1
            dbg._pdebug("unblocked")

            if reg_expecting == 0:
                # done!
                dbg._pdebug("successfully registered to session daemon(s)")
                break

            cur_timeout -= t2 - t1

            if cur_timeout <= 0:
                # timeout
                dbg._pdebug("ran out of time")
                break
        except queue.Empty:
            dbg._pdebug("ran out of time")
            break

    dbg._pdebug("leaving")