// File: /workspace/github_c_makefile_star2_O3/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 0xbd21(%rip), %r12      # 0xcff8
movq (%r12), %rcx
leaq 0x80b6(%rip), %rdi      # 0x9398
pushq $0xb
popq %rsi
pushq $0x1
popq %rdx
callq 0x1250
movq (%r12), %rdi
movq 0xbea0(%rip), %rdx      # 0xd198
leaq 0x80a5(%rip), %rsi      # 0x93a4
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x1170
movq (%r12), %rdi
movq 0xbe7d(%rip), %rdx      # 0xd190
leaq 0x80a4(%rip), %rsi      # 0x93be
xorl %eax, %eax
callq 0x1170
movq (%r12), %rdi
movq 0xbe7c(%rip), %rdx      # 0xd1a8
leaq 0x80a5(%rip), %rsi      # 0x93d8
xorl %eax, %eax
callq 0x1170
movslq 0xbe47(%rip), %rdx      # 0xd188
movq %rdx, %r15
subq 0xbe65(%rip), %r15      # 0xd1b0
movq (%r12), %rdi
leaq 0x809c(%rip), %rsi      # 0x93f2
xorl %eax, %eax
callq 0x1170
movq (%r12), %rdi
leaq 0x80a2(%rip), %rsi      # 0x940a
movq %r15, %rdx
xorl %eax, %eax
callq 0x1170
movslq 0xbe0f(%rip), %rax      # 0xd188
movq %rax, 0xbe30(%rip)      # 0xd1b0
rdtsc
movq %rsp, %rdi
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x1120
movl 0xbdf1(%rip), %eax      # 0xd188
movl $0x0, %r13d
movl $0x0, %r15d
testl %eax, %eax
jle 0x14a8
xorl %r15d, %r15d
movq 0xbdcb(%rip), %rcx      # 0xd180
movl $0x3ff, %edi            # imm = 0x3FF
movabsq $0x45d964b801, %rsi     # imm = 0x45D964B801
xorl %r13d, %r13d
xorl %ebx, %ebx
movq (%rcx,%r15,8), %r12
testq %r12, %r12
je 0x152a
cmpq $0x0, 0x8(%r12)
je 0x148f
movq 0x2440(%r12), %rdx
shrq $0xe, %rdx
cmpl %edi, %edx
cmovael %edi, %edx
incl (%rsp,%rdx,4)
movq %r14, %rdx
subq 0x2430(%r12), %rdx
cmpq %rsi, %rdx
jl 0x1492
rdtsc
shlq $0x20, %rdx
movl %eax, %eax
orq %rdx, %rax
leaq 0xd994(%rip), %rcx      # 0xedb0
movq %rax, 0x70(%rcx)
leaq 0x8021(%rip), %rax      # 0x9448
leaq 0xdc82(%rip), %rcx      # 0xf0b0
movq %rax, 0x70(%rcx)
movq 0x8(%r12), %rdi
movq %r14, %rbp
movl %ebx, %r14d
movq %rsi, %rbx
callq 0x84d7
movl $0x3ff, %edi            # imm = 0x3FF
movq %rbx, %rsi
movl %r14d, %ebx
movq %rbp, %r14
rdtsc
movl %eax, %eax
leaq 0xd952(%rip), %rcx      # 0xedb0
subq 0x70(%rcx), %rax
shlq $0x20, %rdx
leaq 0xdb43(%rip), %rcx      # 0xefb0
addq 0x70(%rcx), %rax
addq %rdx, %rax
movq %rax, 0x70(%rcx)
andq $0x0, 0x8(%r12)
incl %ebx
movq 0xbcf9(%rip), %rcx      # 0xd180
movl 0xbcfb(%rip), %eax      # 0xd188
jmp 0x1492
incl %r13d
incq %r15
movslq %eax, %rdx
cmpq %rdx, %r15
jl 0x13c9
movq 0xbb50(%rip), %r12      # 0xcff8
movq (%r12), %rdi
leaq 0x7fa2(%rip), %rsi      # 0x9455
movl %r15d, %edx
xorl %eax, %eax
callq 0x1170
movl %r13d, %eax
cvtsi2ss %rax, %xmm0
movl %r15d, %eax
cvtsi2ss %rax, %xmm1
movq (%r12), %rdi
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x7f8d(%rip), %rsi      # 0x946d
movl %r13d, %edx
movb $0x1, %al
callq 0x1170
movq (%r12), %rdi
addl %ebx, %r13d
subl %r13d, %r15d
leaq 0x7f91(%rip), %rsi      # 0x948c
movl %r15d, %edx
xorl %eax, %eax
callq 0x1170
movq (%r12), %rdi
leaq 0x7f94(%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 0x1170
leaq 0x7ef3(%rip), %rdi      # 0x9424
leaq 0x7b34(%rip), %rsi      # 0x906c
leaq 0x7eef(%rip), %rcx      # 0x942e
movl $0x2b3, %edx            # imm = 0x2B3
callq 0x1110