~humaid/ns2-trace-go

ref: a284d8fd0d6f7d8a0782cdece0977c8d4a28f524 ns2-trace-go/trace.go -rw-r--r-- 6.0 KiB
a284d8fdHumaid AlQassimi Add AGPL license 1 year, 10 months ago
                                                                                
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main

import (
	"bufio"
	"strconv"
	"strings"
)

// EventType represents the event type of a trace entry.
type EventType int

const (
	REC  = iota // event: receive
	ENQ         // event: enqueue
	DEQ         // event: dequeue
	DROP        // event: drop
	COLL        // event: collision (MAC level)
)

// TraceFlag represents a trace item flag.
type TraceFlag uint8

const (
	NONE = 0x0000
	ECN  = 0x0001 // Explicit Congestion Notification echo is enabled.
	PRI  = 0x0002 // Priority in IP header is enabled.
	CONA = 0x0004 // Congestion action.
	TCPF = 0x0008 // TCP fast start is used.
	ECNO = 0x0010 // Explicit Congestion Notification is on.
)

// TraceItem represents a trace file entry.
type TraceItem struct {
	Event          EventType
	Time           float64
	FromNode       int
	ToNode         int
	PacketType     string
	PacketSize     int
	Flags          int
	FlowID         int
	SourceAddr     Address
	DestAddr       Address
	SequenceNum    int
	UniquePacketID int
}

// Address holds the NS2 pseudo-address.
type Address struct {
	Address int
	Port    int
}

type JitterStat struct {
	FromNode   int
	ToNode     int
	PacketType string
	oldTime    float64         // 1
	oldSeq     float64         // 2
	Jitter     map[int]float64 // (sequence num, jitter)
}

type TraceStats struct {
	TotalEntries    int
	ReceivedPackets int
	DroppedPackets  int
	Collisions      int
	LostPackets     int
	Throughput      float32
	AvgHops         float32
	AvgDelay        float32
	ActiveNodes     int
	TotalBandwidth  int
	NetworkTime     float64
}

func CalculateStats(traces []*TraceItem) (stat TraceStats) {
	var enq int
	var hop float32
	var nodes []int
	var totalSent int
	//var packetsID []int
	var delaySum float32
	var delayCount int
	sentTime := make(map[int]float32) // unique packet id, time sent
	for _, trace := range traces {
		if !(trace.PacketType == "tcp" || trace.PacketType == "udp" || trace.PacketType == "cbr") {
			continue
		}
		stat.TotalEntries++
		switch trace.Event {
		case REC:
			stat.ReceivedPackets++
		case DROP:
			stat.DroppedPackets++
		case ENQ:
			enq++
		case DEQ:
			// This allows us to accurately calculate hops
			totalSent++
			if enq > 0 {
				enq--
				hop++
			}
		case COLL:
			stat.Collisions++
		default:
			continue
		}
		if !hasInt(nodes, trace.FromNode) {
			nodes = append(nodes, trace.FromNode)
			stat.ActiveNodes++
		}
		if !hasInt(nodes, trace.ToNode) {
			nodes = append(nodes, trace.ToNode)
			stat.ActiveNodes++
		}

		stat.TotalBandwidth += trace.PacketSize
		if trace.Time > stat.NetworkTime {
			stat.NetworkTime = trace.Time
		}
		if trace.Event == ENQ {
			if _, ok := sentTime[trace.UniquePacketID]; !ok {
				sentTime[trace.UniquePacketID] = float32(trace.Time)
			}
		}
		if trace.Event == REC {
			delayCount++
			delaySum += float32(trace.Time) - sentTime[trace.UniquePacketID]
		}
	}
	stat.LostPackets = (totalSent - stat.ReceivedPackets)
	stat.Throughput = (float32(stat.ReceivedPackets) / float32(totalSent)) * 100
	stat.AvgHops = (hop / float32(stat.ReceivedPackets))
	stat.AvgDelay = (delaySum / float32(delayCount))
	return
}

func hasInt(ints []int, s int) bool {
	for i := range ints {
		if i == s {
			return true
		}
	}
	return false
}

func CalculateJitters(traces []*TraceItem) (s []*JitterStat) {
	for _, trace := range traces {
		if trace.Event != REC {
			continue
		}
		if !(trace.PacketType == "tcp" || trace.PacketType == "udp" || trace.PacketType == "cbr") {
			continue
		}
		var js *JitterStat = nil
		index := -1
		for i, stat := range s {
			if stat.FromNode == trace.FromNode && stat.ToNode == trace.ToNode &&
				stat.PacketType == trace.PacketType {
				js = stat
				index = i
			}
		}

		if js == nil {
			js = &JitterStat{
				FromNode:   trace.FromNode,
				ToNode:     trace.ToNode,
				PacketType: trace.PacketType,
				oldTime:    0,
				oldSeq:     0,
				Jitter:     make(map[int]float64),
			}
		}

		diff := float64(trace.SequenceNum) - js.oldSeq
		if diff == 0 {
			diff = 1
		}
		if diff > 0 {
			js.Jitter[trace.SequenceNum] = ((trace.Time - js.oldTime) / diff)
			js.oldTime = float64(trace.Time)
			js.oldSeq = float64(trace.SequenceNum)
		}
		if index != -1 {
			s = removeJitter(s, index)
		}

		s = append(s, js)
	}
	return
}

func removeJitter(slice []*JitterStat, s int) []*JitterStat {
	return append(slice[:s], slice[s+1:]...)
}

// GetTracesFromBuffer takes an file buffer and converts it into a list of
// TraceItem.
func GetTracesFromBuffer(scanner *bufio.Scanner) ([]*TraceItem, error) {
	var traces []*TraceItem
	for scanner.Scan() {
		parts := strings.Split(scanner.Text(), " ")
		if len(parts) == 12 {
			ti := new(TraceItem)

			// Event
			switch parts[0] {
			case "r":
				ti.Event = REC
			case "+":
				ti.Event = ENQ
			case "-":
				ti.Event = DEQ
			case "d":
				ti.Event = DROP
			}

			// Time
			time, err := strconv.ParseFloat(parts[1], 64)
			if err != nil {
				return nil, err
			}
			ti.Time = time

			// FromNode
			var i int
			i, err = strconv.Atoi(parts[2])
			if err != nil {
				return nil, err
			}
			ti.FromNode = i

			// ToNode
			i, err = strconv.Atoi(parts[3])
			if err != nil {
				return nil, err
			}
			ti.ToNode = i

			// Packet type
			ti.PacketType = parts[4]

			// Packet size
			i, err = strconv.Atoi(parts[5])
			if err != nil {
				return nil, err
			}
			ti.PacketSize = i

			// TODO flags

			// Flow ID
			i, err = strconv.Atoi(parts[7])
			if err != nil {
				return nil, err
			}
			ti.FlowID = i

			// Source Address
			source := strings.Split(parts[8], ".")
			s1, _ := strconv.Atoi(source[0])
			s2, _ := strconv.Atoi(source[1])
			ti.SourceAddr = Address{s1, s2}

			// Destination Address
			dest := strings.Split(parts[9], ".")
			d1, _ := strconv.Atoi(dest[0])
			d2, _ := strconv.Atoi(dest[1])
			ti.DestAddr = Address{d1, d2}

			// Sequence Number
			i, err = strconv.Atoi(parts[10])
			if err != nil {
				return nil, err
			}
			ti.SequenceNum = i

			i, err = strconv.Atoi(parts[11])
			if err != nil {
				return nil, err
			}
			ti.UniquePacketID = i
			traces = append(traces, ti)
		}
	}

	return traces, nil
}