// File: /workspace/github_c_makefile_star2_O2/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 $0x1018, %rsp           # imm = 0x1018
movq %rdi, %rbx
movq 0x9dc0(%rip), %r15      # 0xbff8
movq (%r15), %rcx
leaq 0x6156(%rip), %rdi      # 0x8398
pushq $0xb
popq %rsi
pushq $0x1
popq %rdx
callq 0x1250
movq (%r15), %rdi
movq 0x9f41(%rip), %rdx      # 0xc198
leaq 0x6146(%rip), %rsi      # 0x83a4
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x1170
movq (%r15), %rdi
movq 0x9f1e(%rip), %rdx      # 0xc190
leaq 0x6145(%rip), %rsi      # 0x83be
xorl %eax, %eax
callq 0x1170
movq (%r15), %rdi
movq 0x9f1e(%rip), %rdx      # 0xc1a8
leaq 0x6147(%rip), %rsi      # 0x83d8
xorl %eax, %eax
callq 0x1170
movslq 0x9ee9(%rip), %rdx      # 0xc188
movq %rdx, %r14
subq 0x9f07(%rip), %r14      # 0xc1b0
movq (%r15), %rdi
leaq 0x613f(%rip), %rsi      # 0x83f2
xorl %eax, %eax
callq 0x1170
movq (%r15), %rdi
leaq 0x6146(%rip), %rsi      # 0x840a
movq %r14, %rdx
xorl %eax, %eax
callq 0x1170
movslq 0x9eb3(%rip), %rax      # 0xc188
movq %rax, 0x9ed4(%rip)      # 0xc1b0
rdtsc
leaq 0x10(%rsp), %rdi
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x1120
movl $0x3ff, %ebp            # imm = 0x3FF
movabsq $0x45d964b801, %r12     # imm = 0x45D964B801
xorl %r14d, %r14d
xorl %esi, %esi
movl $0x0, 0x4(%rsp)
movslq 0x9e76(%rip), %rax      # 0xc188
movq 0x9e67(%rip), %rcx      # 0xc180
movq %r13, 0x8(%rsp)
subl 0x4(%rsp), %r13d
cmpq %rax, %r14
jge 0x23c4
movq (%rcx,%r14,8), %r15
testq %r15, %r15
je 0x2448
incq %r14
cmpq $0x0, 0x8(%r15)
je 0x2368
movq 0x2440(%r15), %rdx
shrq $0xe, %rdx
cmpl %ebp, %edx
cmovael %ebp, %edx
incl 0x10(%rsp,%rdx,4)
movq %rbx, %rdx
subq 0x2430(%r15), %rdx
cmpq %r12, %rdx
jl 0x2323
jmp 0x2371
incl 0x4(%rsp)
decq %r13
jmp 0x2323
rdtsc
shlq $0x20, %rdx
movl %eax, %eax
orq %rdx, %rax
leaq 0xba2d(%rip), %rcx      # 0xddb0
movq %rax, 0x70(%rcx)
leaq 0x60ba(%rip), %rax      # 0x8448
leaq 0xbd1b(%rip), %rcx      # 0xe0b0
movq %rax, 0x70(%rcx)
movq 0x8(%r15), %rdi
movl %esi, %r13d
callq 0x7011
pushq $0xe
popq %rdi
callq 0x1670
movl %r13d, %esi
andq $0x0, 0x8(%r15)
incl %esi
movq 0x8(%rsp), %r13
decl %r13d
jmp 0x230b
movq 0x9c2d(%rip), %rbx      # 0xbff8
movq (%rbx), %rdi
movl %esi, %ebp
leaq 0x607e(%rip), %rsi      # 0x8455
movl %r14d, %edx
xorl %eax, %eax
callq 0x1170
movl 0x4(%rsp), %edx
movl %edx, %eax
cvtsi2ss %rax, %xmm0
movl %r14d, %eax
cvtsi2ss %rax, %xmm1
movq (%rbx), %rdi
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x6067(%rip), %rsi      # 0x846d
movb $0x1, %al
callq 0x1170
movq (%rbx), %rdi
addl %r14d, %r13d
leaq 0x6072(%rip), %rsi      # 0x848c
movl %r13d, %edx
xorl %eax, %eax
callq 0x1170
movq (%rbx), %rdi
leaq 0x6076(%rip), %rsi      # 0x84a4
movl %ebp, %edx
xorl %eax, %eax
addq $0x1018, %rsp           # imm = 0x1018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1170
leaq 0x5fd5(%rip), %rdi      # 0x8424
leaq 0x5c16(%rip), %rsi      # 0x806c
leaq 0x5fd1(%rip), %rcx      # 0x842e
movl $0x2b3, %edx            # imm = 0x2B3
callq 0x1110