Skip to content

Commit

Permalink
added monotonic time converter
Browse files Browse the repository at this point in the history
  • Loading branch information
adubovikov committed Jun 3, 2023
1 parent 845aaf1 commit d59a74d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ assets_nocore: \
build: \
.checkver_$(CMD_GO) \
assets
CGO_ENABLED=0 $(CMD_GO) build -tags $(TARGET_TAG) -ldflags "-w -s -X 'rtcagent/cli/cmd.GitVersion=$(TARGET_TAG)_$(UNAME_M):$(VERSION):[CORE]'" -o bin/rtcagent .
CGO_ENABLED=1 $(CMD_GO) build -tags $(TARGET_TAG) -ldflags "-extldflags '-static' -w -s -X 'rtcagent/cli/cmd.GitVersion=$(TARGET_TAG)_$(UNAME_M):$(VERSION):[CORE]'" -tags netgo -installsuffix netgo -o bin/rtcagent .


# FOR NON-CORE
Expand All @@ -375,7 +375,7 @@ build_nocore: \
.checkver_$(CMD_GO) \
assets_nocore \
ebpf_nocore
CGO_ENABLED=0 $(CMD_GO) build -tags $(TARGET_TAG) -ldflags "-w -s -X 'rtcagent/cli/cmd.GitVersion=$(TARGET_TAG)_$(UNAME_M):$(VERSION):$(UNAME_R)' -X 'main.enableCORE=false'" -o bin/rtcagent .
CGO_ENABLED=1 $(CMD_GO) build -tags $(TARGET_TAG) -ldflags "-extldflags '-static' -w -s -X 'rtcagent/cli/cmd.GitVersion=$(TARGET_TAG)_$(UNAME_M):$(VERSION):$(UNAME_R)' -X 'main.enableCORE=false'" -tags netgo -installsuffix netgo -o bin/rtcagent .

# Format the code
format:
Expand Down
10 changes: 6 additions & 4 deletions user/event/event_freeswitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"fmt"
"net"
"rtcagent/outdata/hep"
"time"
monotonic "rtcagent/user/time"
)

type IpAddrFS struct {
Expand Down Expand Up @@ -163,8 +163,10 @@ func (kem *FreeSwitchEvent) String() string {
connInfo = fmt.Sprintf("%sUNKNOW_%d%s", COLORRED, kem.DataType, COLORRESET)
}

s := fmt.Sprintf("PID:%d, Comm:%s, TID:%d, %s, Time:%d, SrcIP: %s, SrcPort: %d, DstIP: %s, DstPort: %d, Payload:\n%s%s, \n%s", kem.Pid, bytes.TrimSpace(kem.Comm[:]), kem.Tid, connInfo,
kem.Timestamp, srcIP, srcPort, dstIP, dstPort, perfix, string(kem.Data[:kem.DataLen]), COLORRESET)
date := monotonic.GetRealTime(kem.Timestamp)

s := fmt.Sprintf("PID:%d, Comm:%s, TID:%d, %s, TimeML: %d, RealTime: %s, SrcIP: %s, SrcPort: %d, DstIP: %s, DstPort: %d, Payload:\n%s%s, \n%s", kem.Pid, bytes.TrimSpace(kem.Comm[:]), kem.Tid, connInfo,
kem.Timestamp, date.String(), srcIP, srcPort, dstIP, dstPort, perfix, string(kem.Data[:kem.DataLen]), COLORRESET)

return s
}
Expand All @@ -186,7 +188,7 @@ func (kem *FreeSwitchEvent) GenerateHEP() ([]byte, error) {
srcIP := net.IP(dst.IP.Addr[:4])
dstIP := net.IP(dst.IP.Addr[:4])

var date time.Time
date := monotonic.GetRealTime(kem.Timestamp)

hepPacket := hep.Packet{
Version: 0x02,
Expand Down
11 changes: 6 additions & 5 deletions user/event/event_kamailio.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"fmt"
"net"
"rtcagent/outdata/hep"
"time"
monotonic "rtcagent/user/time"
)

const MaxDataSize = 1024 * 4
Expand Down Expand Up @@ -186,9 +186,10 @@ func (kem *KamailioEvent) String() string {

srcIP := net.IP(t.SrcIP.Addr[:4])
dstIP := net.IP(t.DstIP.Addr[:4])

addr := fmt.Sprintf("%s:%d", srcIP.String(), t.SrcPort)

date := monotonic.GetRealTime(kem.Timestamp)

var perfix, connInfo string
switch AttachType(kem.DataType) {
case ProbeEntry:
Expand All @@ -201,8 +202,8 @@ func (kem *KamailioEvent) String() string {
connInfo = fmt.Sprintf("%sUNKNOW_%d%s", COLORRED, kem.DataType, COLORRESET)
}

s := fmt.Sprintf("PID:%d, Comm:%s, TID:%d, %s, Time:%d, SrcIP: %s, SrcPort: %d, DstIP: %s, DstPort: %d, Payload:\n%s%s%s", kem.Pid, bytes.TrimSpace(kem.Comm[:]), kem.Tid, connInfo,
kem.Timestamp,
s := fmt.Sprintf("PID:%d, Comm:%s, TID:%d, %s, TimeML: %d, RealTime: %s, SrcIP: %s, SrcPort: %d, DstIP: %s, DstPort: %d, Payload:\n%s%s%s", kem.Pid, bytes.TrimSpace(kem.Comm[:]), kem.Tid, connInfo,
kem.Timestamp, date.String(),
srcIP.String(), t.SrcPort, dstIP.String(), t.DstPort, perfix, string(kem.Data[:kem.DataLen]), COLORRESET)

//s := fmt.Sprintf("PID:%d, Comm:%s, TID:%d, %s, SrcPort: %d, DstPort:%d, SrcIPv6: %d, Mask: %d, Payload:\n%s%s%s", kem.Pid, bytes.TrimSpace(kem.Comm[:]), kem.Tid, connInfo,
Expand All @@ -227,7 +228,7 @@ func (kem *KamailioEvent) GenerateHEP() ([]byte, error) {
srcIP := net.IP(t.SrcIP.Addr[:4])
dstIP := net.IP(t.DstIP.Addr[:4])

var date time.Time
date := monotonic.GetRealTime(kem.Timestamp)

hepPacket := hep.Packet{
Version: 0x02,
Expand Down
28 changes: 28 additions & 0 deletions user/time/monotonic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package monotonic

import (
"time"
)

/*
#include <time.h>
static unsigned long long get_nsecs(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (unsigned long long)ts.tv_sec * 1000000000UL + ts.tv_nsec;
}
*/
import "C"

func GetTime() (uint64, int64) {
monotonic := uint64(C.get_nsecs())
timestamp := time.Now().UTC().UnixNano()
return monotonic, timestamp
}

func GetRealTime(capTime uint64) time.Time {
monotonic := uint64(C.get_nsecs())
timestamp := time.Now().UTC().UnixNano()
return time.Unix(0, (timestamp - int64(monotonic-capTime)))
}

0 comments on commit d59a74d

Please sign in to comment.