Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0dc4c7a3c9e3a8946b9886cbf3643f2dc36a4964 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*******************************************************************************
 * Copyright (c) 2018 Ecole Polytechnique de Montreal
 *
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package org.eclipse.tracecompass.incubator.internal.ftrace.core.event;

import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.analysis.os.linux.core.kernel.LinuxValues;
import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
import org.eclipse.tracecompass.tmf.core.event.TmfEventField;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Ftrace field class
 *
 * @author Guillaume Champagne
 * @author Alexis-Maurer Fortin
 * @author Hugo Genesse
 * @author Pierre-Yves Lajoie
 * @author Eva Terriault
 */
@NonNullByDefault
public class GenericFtraceField {

    private static final Pattern KEYVAL_PATTERN = Pattern.compile("(?<key>[^\\s=\\[\\]]+)=(?<val>[^\\s=\\[\\]]+)"); //$NON-NLS-1$
    private static final String KEYVAL_KEY_GROUP = "key"; //$NON-NLS-1$
    private static final String KEYVAL_VAL_GROUP = "val"; //$NON-NLS-1$

    private static final double SECONDS_TO_NANO = 1000000000.0;
    private static final Map<Character, @NonNull Long> PREV_STATE_LUT;

    static {
        ImmutableMap.Builder<Character, @NonNull Long> builder = new ImmutableMap.Builder<>();

        builder.put('R', (long) LinuxValues.TASK_STATE_RUNNING);
        builder.put('S', (long) LinuxValues.TASK_INTERRUPTIBLE);
        builder.put('D', (long) LinuxValues.TASK_INTERRUPTIBLE);
        builder.put('T', (long) LinuxValues.TASK_STOPPED__);
        builder.put('t', (long) LinuxValues.TASK_TRACED__);
        builder.put('X', (long) LinuxValues.EXIT_ZOMBIE);
        builder.put('x', (long) LinuxValues.EXIT_ZOMBIE);
        builder.put('Z', (long) LinuxValues.EXIT_DEAD);
        builder.put('P', (long) LinuxValues.TASK_DEAD);
        builder.put('I', (long) LinuxValues.TASK_WAKEKILL);
        PREV_STATE_LUT = builder.build();
    }

    private final Long fTs;
    private String fName;
    private final Integer fCpu;
    private @Nullable Integer fTid;
    private @Nullable Integer fPid;
    private ITmfEventField fContent;

    /**
     * Constructor
     *
     * @param name   event name
     * @param cpu    the cpu number
     * @param ts     the timestamp in ns
     * @param pid    the process id
     * @param tid    the threadId
     * @param fields event fields (arguments)
     */
    public GenericFtraceField(String name, Integer cpu, Long ts, @Nullable Integer pid, @Nullable Integer tid, Map<String, Object> fields) {
        fName = name;
        fCpu = cpu;
        fPid = pid;
        fTid = tid;
        ITmfEventField[] array = fields.entrySet().stream()
                .map(entry -> new TmfEventField(entry.getKey(), entry.getValue(), null))
                .toArray(ITmfEventField[]::new);
        fContent = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, fields, array);
        fTs = ts;
    }

    /**
     * Parse a line from an ftrace ouput file
     *
     * @param line The string to parse
     * @return An event field
     */
    public static @Nullable GenericFtraceField parseLine(String line) {
        Matcher matcher = IGenericFtraceConstants.FTRACE_PATTERN.matcher(line);
        if (matcher.matches()) {
            Integer pid = Integer.parseInt(matcher.group(IGenericFtraceConstants.FTRACE_PID_GROUP));
            Integer tid = pid;
            Integer cpu = Integer.parseInt(matcher.group(IGenericFtraceConstants.FTRACE_CPU_GROUP));
            Double timestampInSec = Double.parseDouble(matcher.group(IGenericFtraceConstants.FTRACE_TIMESTAMP_GROUP));
            Long timestampInNano = (long) (timestampInSec * SECONDS_TO_NANO);

            String name = matcher.group(IGenericFtraceConstants.FTRACE_NAME_GROUP);
            name = name.trim();
            name = name.substring(0, name.length() - 1);

            String attributes = matcher.group(IGenericFtraceConstants.FTRACE_DATA_GROUP);

            /*
             * There's no distinction between pid and tid in scheduling events. However,when there's a mismatch
             * between the tgid and the pid, we know the event happened on a thread and that
             * the tgid is the actual pid, and the pid the tid.
             */
            String tgid = matcher.group(IGenericFtraceConstants.FTRACE_TGID_GROUP);
            if (tgid != null) {
                Integer tgidNumeric = Integer.parseInt(tgid);
                if (!tgidNumeric.equals(pid)) {
                    pid = tgidNumeric;
                }
            }


            Map<@NonNull String, @NonNull Object> fields = new HashMap<>();
            fields.put(IGenericFtraceConstants.TIMESTAMP, timestampInNano);
            fields.put(IGenericFtraceConstants.NAME, name);

            Matcher keyvalMatcher = KEYVAL_PATTERN.matcher(attributes);
            while (keyvalMatcher.find()) {
                String key = keyvalMatcher.group(KEYVAL_KEY_GROUP);
                String value = keyvalMatcher.group(KEYVAL_VAL_GROUP);
                if (value != null) {
                    // This is a temporary solution. Refactor suggestions are welcome.
                    if (key.equals("prev_state")) { //$NON-NLS-1$
                        fields.put(key, parsePrevStateValue(value));
                    } else if (StringUtils.isNumeric(value)) {
                        if (key.equals("parent_pid") && name.equals("sched_process_fork")) {//$NON-NLS-1$ //$NON-NLS-2$
                            key = "pid"; //$NON-NLS-1$
                        }
                        fields.put(key, Long.parseUnsignedLong(value));
                    } else {
                        fields.put(key, decodeString(value));
                    }
                }
            }
            return new GenericFtraceField(name, cpu, timestampInNano, pid, tid, fields);
        }
        return null;
    }

    private static Object decodeString(String val) {
        try {
            if (val.startsWith("0x") || val.startsWith("0X")) { //$NON-NLS-1$ //$NON-NLS-2$
                // Chances are this is an hexadecimal string. Parse the value
                return Long.parseUnsignedLong(val.substring(2), 16);
            }
        } catch (NumberFormatException e) {
            // Fall back to returning the string
        }
        return val;
    }

    /**
     * Get the event content
     *
     * @return the event content
     */
    public ITmfEventField getContent() {
        return fContent;
    }

    /**
     * Set the event's content
     *
     * @param fields Map of field values
     */
    public void setContent(Map<String, Object> fields) {
        ITmfEventField[] array = fields.entrySet().stream()
                .map(entry -> new TmfEventField(entry.getKey(), entry.getValue(), null))
                .toArray(ITmfEventField[]::new);
        fContent = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, fields, array);
    }

    /**
     * Get the name of the event
     *
     * @return the event name
     */
    public String getName() {
        return fName;
    }

    /**
     * Set the event's name
     *
     * @param name New name of the event
     */
    public void setName(String name) {
        fName = name;
    }

    /**
     * Get the TID of the event
     *
     * @return the event TID
     */
    public @Nullable Integer getTid() {
        return fTid;
    }

    /**
     * Set the TID of the event
     *
     * @param tid The new tid
     */
    public void setTid(Integer tid) {
        fTid = tid;
    }

    /**
     * Get the timestamp
     *
     * @return the timestamp in ns
     */
    public Long getTs() {
        return fTs;
    }

    /**
     * Get pid
     *
     * @return the process ID
     */
    @Nullable
    public Integer getPid() {
        return fPid;
    }

    /**
     * Set the PID of the event
     *
     * @param pid The new pid
     */
    public void setPid(Integer pid) {
        fPid = pid;
    }

    /**
     * Get the cpu number
     *
     * @return the cpu number
     */
    public Integer getCpu() {
        return fCpu;
    }

    /**
     * Parse the prev_state field on sched_switch event depending on wether it is a number or a character.
     *
     *
     * @return the state as a Long
     */
    private static Long parsePrevStateValue(String value) {
        Long state = 0L;
        if (StringUtils.isNumeric(value)) {
            state = Long.parseUnsignedLong(value);
        } else {
            state = PREV_STATE_LUT.getOrDefault(value.charAt(0), 0L);
        }
        return state;
    }
}

Back to the top