// File: /workspace/github_c_makefile_star2_O1/fmadio[P]pcap_flow/tcpstream.c
// Function: fTCPStream_Dump
void fTCPStream_Dump(u64 TS)
{
	static u64 LastStreamCnt = 0;

	fprintf(stderr, "TCPStream:\n");
	fprintf(stderr, "  TotalPkts     : %16lli\n", s_TotalPkt);
	fprintf(stderr, "  TotalByte     : %16lli\n", s_TotalByte);

	fprintf(stderr, "  OOO Packets   : %16lli\n", s_TCPBufferPacketTotal);

	s64 NewStreamCnt = s_StreamCnt - LastStreamCnt;
	fprintf(stderr, "  StreamCnt     : %16i\n", s_StreamCnt);
	fprintf(stderr, "  StreamNew     : %16lli\n", NewStreamCnt);

	LastStreamCnt = s_StreamCnt;

	u32 StreamCnt 		= 0;
	u32 StreamInactive 	= 0;
	u32 StreamClose 	= 0;
	u64 TSC				= rdtsc();

	u32 Histo[1024];
	memset(Histo, 0, sizeof(Histo) );

	// garbage collect old streams
	for (int i=0; i < s_StreamCnt; i++)
	{
		TCPStream_t* S = s_Stream[i];
		assert(S != NULL);

		StreamCnt++;

		// stream is inactive
		if (!S->File)
		{
			StreamInactive++;
			continue;
		}

		u32 Index = S->WritePos / (16*1024);
		if (Index > 1023) Index = 1023;
		Histo[Index]++;

		// candidate for closing
		// no activity in the last 5 min 
		s64 dTS = TS - S->LastTS; 
		if (dTS > 60e9 * 5)
		{
			fProfile_Start(14, "TCP IO Close");
			fFile_Close(S->File);
			fProfile_Stop(14);

			S->File = NULL;

			// release 
			StreamClose++;
		}
	}

	/*
	for (int i=0; i < 1024; i++)
	{
		if (Histo[i] == 0) continue;
		printf("%8i B : Cnt %8i\n", i*16*1024, Histo[i]);
	}
	*/

	fprintf(stderr, "  StreamFound   : %16i\n", StreamCnt);
	fprintf(stderr, "  StreamInactive: %16i (%.3f)\n", StreamInactive, StreamInactive / (float)StreamCnt);
	fprintf(stderr, "  StreamActive  : %16i\n", StreamCnt - StreamInactive - StreamClose);
	fprintf(stderr, "  StreamClose   : %16i\n", StreamClose); 
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1008, %rsp           # imm = 0x1008
movq %rdi, %r14
movq 0xbd1b(%rip), %r12      # 0xcff8
movq (%r12), %rcx
leaq 0x80b0(%rip), %rdi      # 0x9398
pushq $0xb
popq %rsi
pushq $0x1
popq %rdx
callq 0x1240
movq (%r12), %rdi
movq 0xbe9a(%rip), %rdx      # 0xd198
leaq 0x809f(%rip), %rsi      # 0x93a4
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x1160
movq (%r12), %rdi
movq 0xbe77(%rip), %rdx      # 0xd190
leaq 0x809e(%rip), %rsi      # 0x93be
xorl %eax, %eax
callq 0x1160
movq (%r12), %rdi
movq 0xbe76(%rip), %rdx      # 0xd1a8
leaq 0x809f(%rip), %rsi      # 0x93d8
xorl %eax, %eax
callq 0x1160
movslq 0xbe41(%rip), %rdx      # 0xd188
movq %rdx, %r15
subq 0xbe5f(%rip), %r15      # 0xd1b0
movq (%r12), %rdi
leaq 0x8096(%rip), %rsi      # 0x93f2
xorl %eax, %eax
callq 0x1160
movq (%r12), %rdi
leaq 0x809c(%rip), %rsi      # 0x940a
movq %r15, %rdx
xorl %eax, %eax
callq 0x1160
movslq 0xbe09(%rip), %rax      # 0xd188
movq %rax, 0xbe2a(%rip)      # 0xd1b0
rdtsc
movq %rsp, %rdi
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x1120
cmpl $0x0, 0xbdea(%rip)      # 0xd188
movl $0x0, %r13d
movl $0x0, %r15d
jle 0x14a0
xorl %r15d, %r15d
movl $0x3ff, %edx            # imm = 0x3FF
movabsq $0x45d964b801, %rcx     # imm = 0x45D964B801
xorl %r13d, %r13d
xorl %ebx, %ebx
movq 0xbdb2(%rip), %rax      # 0xd180
movq (%rax,%r15,8), %r12
testq %r12, %r12
je 0x1522
cmpq $0x0, 0x8(%r12)
je 0x1483
movq 0x2440(%r12), %rax
shrq $0xe, %rax
cmpl %edx, %eax
cmovael %edx, %eax
incl (%rsp,%rax,4)
movq %r14, %rax
subq 0x2430(%r12), %rax
cmpq %rcx, %rax
jl 0x1486
rdtsc
shlq $0x20, %rdx
movl %eax, %eax
orq %rdx, %rax
leaq 0xd993(%rip), %rdx      # 0xedb0
movq %rax, 0x70(%rdx)
leaq 0x8020(%rip), %rax      # 0x9448
leaq 0xdc81(%rip), %rdx      # 0xf0b0
movq %rax, 0x70(%rdx)
movq 0x8(%r12), %rdi
movq %r14, %rbp
movl %ebx, %r14d
movq %rcx, %rbx
callq 0x8972
movq %rbx, %rcx
movl %r14d, %ebx
movq %rbp, %r14
rdtsc
shlq $0x20, %rdx
movl %eax, %eax
leaq 0xd952(%rip), %rsi      # 0xedb0
subq 0x70(%rsi), %rax
leaq 0xdb47(%rip), %rsi      # 0xefb0
addq 0x70(%rsi), %rax
addq %rdx, %rax
movl $0x3ff, %edx            # imm = 0x3FF
movq %rax, 0x70(%rsi)
andq $0x0, 0x8(%r12)
incl %ebx
jmp 0x1486
incl %r13d
incq %r15
movslq 0xbcf8(%rip), %rax      # 0xd188
cmpq %rax, %r15
jl 0x13c7
movq 0xbb58(%rip), %r12      # 0xcff8
movq (%r12), %rdi
leaq 0x7faa(%rip), %rsi      # 0x9455
movl %r15d, %edx
xorl %eax, %eax
callq 0x1160
movl %r13d, %eax
cvtsi2ss %rax, %xmm0
movl %r15d, %eax
cvtsi2ss %rax, %xmm1
movq (%r12), %rdi
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x7f95(%rip), %rsi      # 0x946d
movl %r13d, %edx
movb $0x1, %al
callq 0x1160
movq (%r12), %rdi
addl %ebx, %r13d
subl %r13d, %r15d
leaq 0x7f99(%rip), %rsi      # 0x948c
movl %r15d, %edx
xorl %eax, %eax
callq 0x1160
movq (%r12), %rdi
leaq 0x7f9c(%rip), %rsi      # 0x94a4
movl %ebx, %edx
xorl %eax, %eax
addq $0x1008, %rsp           # imm = 0x1008
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1160
leaq 0x7efb(%rip), %rdi      # 0x9424
leaq 0x7b3c(%rip), %rsi      # 0x906c
leaq 0x7ef7(%rip), %rcx      # 0x942e
movl $0x2b3, %edx            # imm = 0x2B3
callq 0x1110