// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x1058, %rsp           # imm = 0x1058
movq %rdi, 0x1050(%rsp)
movq 0xd362(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x96fc(%rip), %rsi      # 0xc39c
movb $0x0, %al
callq 0x1150
movq 0xd34a(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq 0xd4f0(%rip), %rdx      # 0x101a8
leaq 0x96e9(%rip), %rsi      # 0xc3a8
movb $0x0, %al
callq 0x1150
movq 0xd32b(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq 0xd4c9(%rip), %rdx      # 0x101a0
leaq 0x96e4(%rip), %rsi      # 0xc3c2
movb $0x0, %al
callq 0x1150
movq 0xd30c(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq 0xd4c2(%rip), %rdx      # 0x101b8
leaq 0x96df(%rip), %rsi      # 0xc3dc
movb $0x0, %al
callq 0x1150
movslq 0xd48d(%rip), %rax      # 0x10198
subq 0xd4ae(%rip), %rax      # 0x101c0
movq %rax, 0x1048(%rsp)
movq 0xd2d7(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movl 0xd46e(%rip), %edx      # 0x10198
leaq 0x96c5(%rip), %rsi      # 0xc3f6
movb $0x0, %al
callq 0x1150
movq 0xd2b9(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq 0x1048(%rsp), %rdx
leaq 0x96bd(%rip), %rsi      # 0xc40e
movb $0x0, %al
callq 0x1150
movslq 0xd439(%rip), %rax      # 0x10198
movq %rax, 0xd45a(%rip)      # 0x101c0
movl $0x0, 0x1044(%rsp)
movl $0x0, 0x1040(%rsp)
movl $0x0, 0x103c(%rsp)
callq 0x1840
movq %rax, 0x1030(%rsp)
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x1110
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0xd3e1(%rip), %eax      # 0x10198
jge 0x2eea
movq 0xd3cc(%rip), %rax      # 0x10190
movslq 0x2c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
je 0x2ddc
jmp 0x2dfb
leaq 0x9645(%rip), %rdi      # 0xc428
leaq 0x927a(%rip), %rsi      # 0xc064
movl $0x2b3, %edx            # imm = 0x2B3
leaq 0x963c(%rip), %rcx      # 0xc432
callq 0x1100
movl 0x1044(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1044(%rsp)
movq 0x20(%rsp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x2e2e
movl 0x1040(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1040(%rsp)
jmp 0x2eda
movq 0x20(%rsp), %rax
movq 0x2440(%rax), %rax
shrq $0xe, %rax
movl %eax, 0x1c(%rsp)
cmpl $0x3ff, 0x1c(%rsp)      # imm = 0x3FF
jbe 0x2e54
movl $0x3ff, 0x1c(%rsp)      # imm = 0x3FF
movl 0x1c(%rsp), %eax
movl 0x30(%rsp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, 0x30(%rsp,%rax,4)
movq 0x1050(%rsp), %rax
movq 0x20(%rsp), %rcx
subq 0x2430(%rcx), %rax
movq %rax, 0x10(%rsp)
cvtsi2sdq 0x10(%rsp), %xmm0
movsd 0x9185(%rip), %xmm1     # 0xc010
ucomisd %xmm1, %xmm0
jbe 0x2ed8
movl $0xe, %edi
leaq 0x95af(%rip), %rsi      # 0xc44c
callq 0x17f0
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0xbb50
movl $0xe, %edi
callq 0x1860
movq 0x20(%rsp), %rax
movq $0x0, 0x8(%rax)
movl 0x103c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x103c(%rsp)
jmp 0x2eda
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x2dad
movq 0xd107(%rip), %rax      # 0xfff8
movq %rax, 0x8(%rsp)
movq (%rax), %rdi
movl 0x1044(%rsp), %edx
leaq 0x9552(%rip), %rsi      # 0xc459
xorl %eax, %eax
callq 0x1150
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movl 0x1040(%rsp), %eax
movl %eax, %edx
cvtsi2ss %rax, %xmm0
movl 0x1044(%rsp), %eax
cvtsi2ss %rax, %xmm1
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x9532(%rip), %rsi      # 0xc471
movb $0x1, %al
callq 0x1150
movq 0xd0ab(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movl 0x1044(%rsp), %edx
subl 0x1040(%rsp), %edx
subl 0x103c(%rsp), %edx
leaq 0x9524(%rip), %rsi      # 0xc490
movb $0x0, %al
callq 0x1150
movq 0xd07e(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movl 0x103c(%rsp), %edx
leaq 0x951d(%rip), %rsi      # 0xc4a8
movb $0x0, %al
callq 0x1150
addq $0x1058, %rsp           # imm = 0x1058
retq
nopw (%rax,%rax)