2 * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
3 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License, version 2.1 only,
7 * as published by the Free Software Foundation.
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 package org
.lttng
.ust
.agent
;
21 import java
.util
.Collection
;
22 import java
.util
.HashSet
;
24 import java
.util
.NavigableMap
;
26 import java
.util
.concurrent
.ConcurrentHashMap
;
27 import java
.util
.concurrent
.ConcurrentSkipListMap
;
28 import java
.util
.concurrent
.atomic
.AtomicInteger
;
30 import org
.lttng
.ust
.agent
.client
.ILttngTcpClientListener
;
31 import org
.lttng
.ust
.agent
.client
.LttngTcpSessiondClient
;
32 import org
.lttng
.ust
.agent
.filter
.FilterChangeNotifier
;
33 import org
.lttng
.ust
.agent
.session
.EventRule
;
36 * Base implementation of a {@link ILttngAgent}.
38 * @author Alexandre Montplaisir
40 * The type of logging handler that should register to this agent
42 public abstract class AbstractLttngAgent
<T
extends ILttngHandler
>
43 implements ILttngAgent
<T
>, ILttngTcpClientListener
{
45 private static final String WILDCARD
= "*";
46 private static final int INIT_TIMEOUT
= 3; /* Seconds */
48 /** The handlers registered to this agent */
49 private final Set
<T
> registeredHandlers
= new HashSet
<T
>();
52 * The trace events currently enabled in the sessions.
54 * The key represents the event name, the value is the ref count (how many
55 * different sessions currently have this event enabled). Once the ref count
56 * falls to 0, this means we can avoid sending log events through JNI
57 * because nobody wants them.
59 * It uses a concurrent hash map, so that the {@link #isEventEnabled} and
60 * read methods do not need to take a synchronization lock.
62 private final Map
<String
, Integer
> enabledEvents
= new ConcurrentHashMap
<String
, Integer
>();
65 * The trace events prefixes currently enabled in the sessions, which means
66 * the event names finishing in *, like "abcd*". We track them separately
67 * from the standard event names, so that we can use {@link String#equals}
68 * and {@link String#startsWith} appropriately.
70 * We track the lone wildcard "*" separately, in {@link #enabledWildcards}.
72 private final NavigableMap
<String
, Integer
> enabledEventPrefixes
=
73 new ConcurrentSkipListMap
<String
, Integer
>();
75 /** Number of sessions currently enabling the wildcard "*" event */
76 private final AtomicInteger enabledWildcards
= new AtomicInteger(0);
78 /** Tracing domain. Defined by the sub-classes via the constructor. */
79 private final Domain domain
;
81 /* Lazy-loaded sessiond clients and their thread objects */
82 private LttngTcpSessiondClient rootSessiondClient
= null;
83 private LttngTcpSessiondClient userSessiondClient
= null;
84 private Thread rootSessiondClientThread
= null;
85 private Thread userSessiondClientThread
= null;
87 /** Indicates if this agent has been initialized. */
88 private boolean initialized
= false;
91 * Constructor. Should only be called by sub-classes via super(...);
94 * The tracing domain of this agent.
96 protected AbstractLttngAgent(Domain domain
) {
101 public Domain
getDomain() {
106 public void registerHandler(T handler
) {
107 synchronized (registeredHandlers
) {
108 if (registeredHandlers
.isEmpty()) {
110 * This is the first handler that registers, we will initialize
115 registeredHandlers
.add(handler
);
120 public void unregisterHandler(T handler
) {
121 synchronized (registeredHandlers
) {
122 registeredHandlers
.remove(handler
);
123 if (registeredHandlers
.isEmpty()) {
124 /* There are no more registered handlers, close the connection. */
130 private void init() {
132 * Only called from a synchronized (registeredHandlers) block, should
133 * not need additional synchronization.
138 String rootClientThreadName
= "Root sessiond client started by agent: " + this.getClass().getSimpleName();
140 rootSessiondClient
= new LttngTcpSessiondClient(this, getDomain().value(), true);
141 rootSessiondClientThread
= new Thread(rootSessiondClient
, rootClientThreadName
);
142 rootSessiondClientThread
.setDaemon(true);
143 rootSessiondClientThread
.start();
145 String userClientThreadName
= "User sessiond client started by agent: " + this.getClass().getSimpleName();
147 userSessiondClient
= new LttngTcpSessiondClient(this, getDomain().value(), false);
148 userSessiondClientThread
= new Thread(userSessiondClient
, userClientThreadName
);
149 userSessiondClientThread
.setDaemon(true);
150 userSessiondClientThread
.start();
152 /* Give the threads' registration a chance to end. */
153 if (!rootSessiondClient
.waitForConnection(INIT_TIMEOUT
)) {
154 userSessiondClient
.waitForConnection(INIT_TIMEOUT
);
163 private void dispose() {
165 * Only called from a synchronized (registeredHandlers) block, should
166 * not need additional synchronization.
168 rootSessiondClient
.close();
169 userSessiondClient
.close();
172 rootSessiondClientThread
.join();
173 userSessiondClientThread
.join();
175 } catch (InterruptedException e
) {
178 rootSessiondClient
= null;
179 rootSessiondClientThread
= null;
180 userSessiondClient
= null;
181 userSessiondClientThread
= null;
183 /* Reset all enabled event counts to 0 */
184 enabledEvents
.clear();
185 enabledEventPrefixes
.clear();
186 enabledWildcards
.set(0);
193 public boolean eventEnabled(EventRule eventRule
) {
194 /* Notify the filter change manager of the command */
195 FilterChangeNotifier
.getInstance().addEventRule(eventRule
);
197 String eventName
= eventRule
.getEventName();
199 if (eventName
.equals(WILDCARD
)) {
200 enabledWildcards
.incrementAndGet();
203 if (eventName
.endsWith(WILDCARD
)) {
204 /* Strip the "*" from the name. */
205 String prefix
= eventName
.substring(0, eventName
.length() - 1);
206 return incrementEventCount(prefix
, enabledEventPrefixes
);
209 return incrementEventCount(eventName
, enabledEvents
);
213 public boolean eventDisabled(String eventName
) {
214 /* Notify the filter change manager of the command */
215 FilterChangeNotifier
.getInstance().removeEventRules(eventName
);
217 if (eventName
.equals(WILDCARD
)) {
218 int newCount
= enabledWildcards
.decrementAndGet();
220 /* Event was not enabled, bring the count back to 0 */
221 enabledWildcards
.incrementAndGet();
227 if (eventName
.endsWith(WILDCARD
)) {
228 /* Strip the "*" from the name. */
229 String prefix
= eventName
.substring(0, eventName
.length() - 1);
230 return decrementEventCount(prefix
, enabledEventPrefixes
);
233 return decrementEventCount(eventName
, enabledEvents
);
237 * Implementation of this method is domain-specific.
240 public abstract Collection
<String
> listAvailableEvents();
243 public boolean isEventEnabled(String eventName
) {
244 /* If at least one session enabled the "*" wildcard, send the event */
245 if (enabledWildcards
.get() > 0) {
249 /* Check if at least one session wants this exact event name */
250 if (enabledEvents
.containsKey(eventName
)) {
254 /* Look in the enabled prefixes if one of them matches the event */
255 String potentialMatch
= enabledEventPrefixes
.floorKey(eventName
);
256 if (potentialMatch
!= null && eventName
.startsWith(potentialMatch
)) {
263 private static boolean incrementEventCount(String eventName
, Map
<String
, Integer
> eventMap
) {
264 synchronized (eventMap
) {
265 Integer count
= eventMap
.get(eventName
);
267 /* This is the first instance of this event being enabled */
268 eventMap
.put(eventName
, Integer
.valueOf(1));
271 if (count
.intValue() <= 0) {
272 /* It should not have been in the map in the first place! */
273 throw new IllegalStateException();
275 /* The event was already enabled, increment its refcount */
276 eventMap
.put(eventName
, Integer
.valueOf(count
.intValue() + 1));
281 private static boolean decrementEventCount(String eventName
, Map
<String
, Integer
> eventMap
) {
282 synchronized (eventMap
) {
283 Integer count
= eventMap
.get(eventName
);
284 if (count
== null || count
.intValue() <= 0) {
286 * The sessiond asked us to disable an event that was not
287 * enabled previously. Command error?
291 if (count
.intValue() == 1) {
293 * This is the last instance of this event being disabled,
294 * remove it from the map so that we stop sending it.
296 eventMap
.remove(eventName
);
300 * Other sessions are still looking for this event, simply decrement
303 eventMap
.put(eventName
, Integer
.valueOf(count
.intValue() - 1));