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
.HashSet
;
22 import java
.util
.LinkedList
;
23 import java
.util
.List
;
25 import java
.util
.NavigableMap
;
27 import java
.util
.concurrent
.ConcurrentHashMap
;
28 import java
.util
.concurrent
.ConcurrentSkipListMap
;
29 import java
.util
.concurrent
.atomic
.AtomicInteger
;
31 import org
.lttng
.ust
.agent
.client
.ILttngTcpClientListener
;
32 import org
.lttng
.ust
.agent
.client
.LttngTcpSessiondClient
;
33 import org
.lttng
.ust
.agent
.filter
.FilterChangeNotifier
;
34 import org
.lttng
.ust
.agent
.session
.EventRule
;
37 * Base implementation of a {@link ILttngAgent}.
39 * @author Alexandre Montplaisir
41 * The type of logging handler that should register to this agent
43 public abstract class AbstractLttngAgent
<T
extends ILttngHandler
>
44 implements ILttngAgent
<T
>, ILttngTcpClientListener
{
46 private static final String WILDCARD
= "*";
47 private static final int INIT_TIMEOUT
= 3; /* Seconds */
49 /** The handlers registered to this agent */
50 private final Set
<T
> registeredHandlers
= new HashSet
<T
>();
53 * The trace events currently enabled in the sessions.
55 * The key represents the event name, the value is the ref count (how many
56 * different sessions currently have this event enabled). Once the ref count
57 * falls to 0, this means we can avoid sending log events through JNI
58 * because nobody wants them.
60 * It uses a concurrent hash map, so that the {@link #isEventEnabled} and
61 * read methods do not need to take a synchronization lock.
63 private final Map
<String
, Integer
> enabledEvents
= new ConcurrentHashMap
<String
, Integer
>();
66 * The trace events prefixes currently enabled in the sessions, which means
67 * the event names finishing in *, like "abcd*". We track them separately
68 * from the standard event names, so that we can use {@link String#equals}
69 * and {@link String#startsWith} appropriately.
71 * We track the lone wildcard "*" separately, in {@link #enabledWildcards}.
73 private final NavigableMap
<String
, Integer
> enabledEventPrefixes
=
74 new ConcurrentSkipListMap
<String
, Integer
>();
76 /** Number of sessions currently enabling the wildcard "*" event */
77 private final AtomicInteger enabledWildcards
= new AtomicInteger(0);
79 /** Tracing domain. Defined by the sub-classes via the constructor. */
80 private final Domain domain
;
82 /* Lazy-loaded sessiond clients and their thread objects */
83 private LttngTcpSessiondClient rootSessiondClient
= null;
84 private LttngTcpSessiondClient userSessiondClient
= null;
85 private Thread rootSessiondClientThread
= null;
86 private Thread userSessiondClientThread
= null;
88 /** Indicates if this agent has been initialized. */
89 private boolean initialized
= false;
92 * Constructor. Should only be called by sub-classes via super(...);
95 * The tracing domain of this agent.
97 protected AbstractLttngAgent(Domain domain
) {
102 public Domain
getDomain() {
107 public void registerHandler(T handler
) {
108 synchronized (registeredHandlers
) {
109 if (registeredHandlers
.isEmpty()) {
111 * This is the first handler that registers, we will initialize
116 registeredHandlers
.add(handler
);
121 public void unregisterHandler(T handler
) {
122 synchronized (registeredHandlers
) {
123 registeredHandlers
.remove(handler
);
124 if (registeredHandlers
.isEmpty()) {
125 /* There are no more registered handlers, close the connection. */
131 private void init() {
133 * Only called from a synchronized (registeredHandlers) block, should
134 * not need additional synchronization.
139 String rootClientThreadName
= "Root sessiond client started by agent: " + this.getClass().getSimpleName();
141 rootSessiondClient
= new LttngTcpSessiondClient(this, getDomain().value(), true);
142 rootSessiondClientThread
= new Thread(rootSessiondClient
, rootClientThreadName
);
143 rootSessiondClientThread
.setDaemon(true);
144 rootSessiondClientThread
.start();
146 String userClientThreadName
= "User sessiond client started by agent: " + this.getClass().getSimpleName();
148 userSessiondClient
= new LttngTcpSessiondClient(this, getDomain().value(), false);
149 userSessiondClientThread
= new Thread(userSessiondClient
, userClientThreadName
);
150 userSessiondClientThread
.setDaemon(true);
151 userSessiondClientThread
.start();
153 /* Give the threads' registration a chance to end. */
154 if (!rootSessiondClient
.waitForConnection(INIT_TIMEOUT
)) {
155 userSessiondClient
.waitForConnection(INIT_TIMEOUT
);
164 private void dispose() {
166 * Only called from a synchronized (registeredHandlers) block, should
167 * not need additional synchronization.
169 rootSessiondClient
.close();
170 userSessiondClient
.close();
173 rootSessiondClientThread
.join();
174 userSessiondClientThread
.join();
176 } catch (InterruptedException e
) {
179 rootSessiondClient
= null;
180 rootSessiondClientThread
= null;
181 userSessiondClient
= null;
182 userSessiondClientThread
= null;
184 /* Reset all enabled event counts to 0 */
185 enabledEvents
.clear();
186 enabledEventPrefixes
.clear();
187 enabledWildcards
.set(0);
194 public boolean eventEnabled(EventRule eventRule
) {
195 /* Notify the filter change manager of the command */
196 FilterChangeNotifier
.getInstance().addEventRule(eventRule
);
198 String eventName
= eventRule
.getEventName();
200 if (eventName
.equals(WILDCARD
)) {
201 enabledWildcards
.incrementAndGet();
204 if (eventName
.endsWith(WILDCARD
)) {
205 /* Strip the "*" from the name. */
206 String prefix
= eventName
.substring(0, eventName
.length() - 1);
207 return incrementEventCount(prefix
, enabledEventPrefixes
);
210 return incrementEventCount(eventName
, enabledEvents
);
214 public boolean eventDisabled(String eventName
) {
215 /* Notify the filter change manager of the command */
216 FilterChangeNotifier
.getInstance().removeEventRules(eventName
);
218 if (eventName
.equals(WILDCARD
)) {
219 int newCount
= enabledWildcards
.decrementAndGet();
221 /* Event was not enabled, bring the count back to 0 */
222 enabledWildcards
.incrementAndGet();
228 if (eventName
.endsWith(WILDCARD
)) {
229 /* Strip the "*" from the name. */
230 String prefix
= eventName
.substring(0, eventName
.length() - 1);
231 return decrementEventCount(prefix
, enabledEventPrefixes
);
234 return decrementEventCount(eventName
, enabledEvents
);
238 public Iterable
<String
> listEnabledEvents() {
239 List
<String
> events
= new LinkedList
<String
>();
241 if (enabledWildcards
.get() > 0) {
242 events
.add(WILDCARD
);
244 for (String prefix
: enabledEventPrefixes
.keySet()) {
245 events
.add(new String(prefix
+ WILDCARD
));
247 events
.addAll(enabledEvents
.keySet());
252 public boolean isEventEnabled(String eventName
) {
253 /* If at least one session enabled the "*" wildcard, send the event */
254 if (enabledWildcards
.get() > 0) {
258 /* Check if at least one session wants this exact event name */
259 if (enabledEvents
.containsKey(eventName
)) {
263 /* Look in the enabled prefixes if one of them matches the event */
264 String potentialMatch
= enabledEventPrefixes
.floorKey(eventName
);
265 if (potentialMatch
!= null && eventName
.startsWith(potentialMatch
)) {
272 private static boolean incrementEventCount(String eventName
, Map
<String
, Integer
> eventMap
) {
273 synchronized (eventMap
) {
274 Integer count
= eventMap
.get(eventName
);
276 /* This is the first instance of this event being enabled */
277 eventMap
.put(eventName
, Integer
.valueOf(1));
280 if (count
.intValue() <= 0) {
281 /* It should not have been in the map in the first place! */
282 throw new IllegalStateException();
284 /* The event was already enabled, increment its refcount */
285 eventMap
.put(eventName
, Integer
.valueOf(count
.intValue() + 1));
290 private static boolean decrementEventCount(String eventName
, Map
<String
, Integer
> eventMap
) {
291 synchronized (eventMap
) {
292 Integer count
= eventMap
.get(eventName
);
293 if (count
== null || count
.intValue() <= 0) {
295 * The sessiond asked us to disable an event that was not
296 * enabled previously. Command error?
300 if (count
.intValue() == 1) {
302 * This is the last instance of this event being disabled,
303 * remove it from the map so that we stop sending it.
305 eventMap
.remove(eventName
);
309 * Other sessions are still looking for this event, simply decrement
312 eventMap
.put(eventName
, Integer
.valueOf(count
.intValue() - 1));