/*
 * GenerateDetachPartitionCommand gets a partition table and returns
 * "ALTER TABLE parent_table DETACH PARTITION partitionName" command.
 */
char *
GenerateDetachPartitionCommand(Oid partitionTableId)
{
	StringInfo detachPartitionCommand = makeStringInfo();

#if (PG_VERSION_NUM >= 100000)
	Oid parentId = InvalidOid;
	char *tableQualifiedName = NULL;
	char *parentTableQualifiedName = NULL;

	if (!PartitionTable(partitionTableId))
	{
		char *relationName = get_rel_name(partitionTableId);

		ereport(ERROR, (errmsg("\"%s\" is not a partition", relationName)));
	}

	parentId = get_partition_parent(partitionTableId);
	tableQualifiedName = generate_qualified_relation_name(partitionTableId);
	parentTableQualifiedName = generate_qualified_relation_name(parentId);

	appendStringInfo(detachPartitionCommand,
					 "ALTER TABLE IF EXISTS %s DETACH PARTITION %s;",
					 parentTableQualifiedName, tableQualifiedName);
#endif

	return detachPartitionCommand->data;
}
/*
 * GenerateAlterTableAttachPartitionCommand returns the necessary command to
 * attach the given partition to its parent.
 */
char *
GenerateAlterTableAttachPartitionCommand(Oid partitionTableId)
{
	StringInfo createPartitionCommand = makeStringInfo();

#if (PG_VERSION_NUM >= 100000)
	char *partitionBoundCString = NULL;

	Oid parentId = InvalidOid;
	char *tableQualifiedName = NULL;
	char *parentTableQualifiedName = NULL;

	if (!PartitionTable(partitionTableId))
	{
		char *relationName = get_rel_name(partitionTableId);

		ereport(ERROR, (errmsg("\"%s\" is not a partition", relationName)));
	}

	parentId = get_partition_parent(partitionTableId);
	tableQualifiedName = generate_qualified_relation_name(partitionTableId);
	parentTableQualifiedName = generate_qualified_relation_name(parentId);

	partitionBoundCString = PartitionBound(partitionTableId);

	appendStringInfo(createPartitionCommand, "ALTER TABLE %s ATTACH PARTITION %s %s;",
					 parentTableQualifiedName, tableQualifiedName,
					 partitionBoundCString);
#endif

	return createPartitionCommand->data;
}
/*
 * IsChildTable returns true if the table is inherited. Note that
 * partition tables inherites by default. However, this function
 * returns false if the given table is a partition.
 */
bool
IsChildTable(Oid relationId)
{
	Relation pgInherits = NULL;
	SysScanDesc scan = NULL;
	ScanKeyData key[1];
	HeapTuple inheritsTuple = NULL;
	bool tableInherits = false;

	pgInherits = heap_open(InheritsRelationId, AccessShareLock);

	ScanKeyInit(&key[0], Anum_pg_inherits_inhrelid,
				BTEqualStrategyNumber, F_OIDEQ,
				ObjectIdGetDatum(relationId));

	scan = systable_beginscan(pgInherits, InvalidOid, false,
							  NULL, 1, key);

	while ((inheritsTuple = systable_getnext(scan)) != NULL)
	{
		Oid inheritedRelationId =
			((Form_pg_inherits) GETSTRUCT(inheritsTuple))->inhrelid;

		if (relationId == inheritedRelationId)
		{
			tableInherits = true;
			break;
		}
	}

	systable_endscan(scan);
	heap_close(pgInherits, AccessShareLock);

	if (tableInherits && PartitionTable(relationId))
	{
		tableInherits = false;
	}

	return tableInherits;
}
Example #4
0
File: GHJ.cpp Project: ruanzx/PSMJ
RC GHJ::Execute()
{
	RC rc = Initialize();
	if(rc!=SUCCESS)
	{
		return rc;
	} 

	DOUBLE cpuTimeBefore = 0;
	DOUBLE cpuTimeAfter = 0;
	DOUBLE cpuTime = 0;
	StopWatch stwTotalTime;
	StopWatch stwJoinTime;
	StopWatch stwPartitionTime;
	UINT64 totalTime = 0;
	UINT64 partitionTime = 0;
	UINT64 joinTime = 0;

	stwTotalTime.Start();
	stwPartitionTime.Start();
	cpuTimeBefore = GetCpuTime();

	PartitionTable(m_Params.RELATION_R_PATH, m_Params.R_KEY_POS, &m_InBuffer, m_BucketBuffer, m_FileHandle);

	/*************************************************************************/ 

	//再次对桶装配过程中需要的全局变量初始化 
	for(UINT partitionIndex=0; partitionIndex < m_PartitionNum; partitionIndex++)
	{   
		ResetPage(&m_BucketPage[partitionIndex], &m_BucketBuffer[partitionIndex]); 
	}

	PartitionTable(m_Params.RELATION_S_PATH, m_Params.S_KEY_POS, &m_InBuffer, m_BucketBuffer, m_FileHandle);

	partitionTime = stwPartitionTime.NowInMilliseconds();
	stwJoinTime.Start();

	HashJoin(&m_Pool);


	cpuTimeAfter = GetCpuTime(); 
	totalTime = stwTotalTime.NowInMilliseconds();
	joinTime = stwJoinTime.NowInMilliseconds();
	cpuTime = cpuTimeAfter - cpuTimeBefore;


	FILE *fp;   
	CHAR *reportFilePath = new CHAR[MAX_PATH];   
	LPWSTR tempReportPath = new TCHAR[MAX_PATH];
	swprintf(tempReportPath, MAX_PATH, L"%s%s", m_Params.WORK_SPACE_PATH, L"GHJ_Report.csv" ); 
	// convert file path to char  
	size_t  count = wcstombs(reportFilePath, tempReportPath, MAX_PATH); 

	CHAR *reportTitle = "Relation Size,Memory Size,Bucket Size,Partition,Read Buffer Size,Write Buffer Size,Total Execute Time(ms),Partition Time(ms),Join Time(ms),CPU Time\n";
	CHAR *reportContent = new CHAR[1024];
	sprintf(reportContent, "%d,%d,%.f,%d,%d,%d,%lld,%lld,%lld,%.f", 
		m_R_FileSize, 
		m_Params.BUFFER_POOL_SIZE/SSD_PAGE_SIZE, 
		m_BucketSize, 
		m_PartitionNum, 
		m_Params.READ_BUFFER_SIZE/SSD_PAGE_SIZE,
		m_Params.WRITE_BUFFER_SIZE/SSD_PAGE_SIZE, 
		totalTime, 
		partitionTime, 
		joinTime, 
		cpuTime);
	fp=fopen(reportFilePath, "w+b"); 
	fprintf(fp, reportTitle);
	fprintf(fp, reportContent);
	fclose(fp);

	delete reportFilePath;
	delete tempReportPath; 
	delete reportContent;

	//连接结束后 删除所有的临时文件 
	LPWSTR tempBucketName = new TCHAR[MAX_PATH];
	for(UINT partitionIndex=0;partitionIndex < m_PartitionNum;partitionIndex++)
	{
		swprintf_s(tempBucketName, MAX_PATH, L"%s%d%s.tmp", m_Params.WORK_SPACE_PATH, partitionIndex, m_Params.RELATION_R_NO_EXT);
		DeleteFile(tempBucketName);

		swprintf_s(tempBucketName, MAX_PATH, L"%s%d%s.tmp", m_Params.WORK_SPACE_PATH, partitionIndex, m_Params.RELATION_S_NO_EXT);
		DeleteFile(tempBucketName); 
	}

	delete tempBucketName;
	delete m_Pool.data;
	CloseHandle(m_hOutFile);

	ShowMB(L"GHJ done"); 

	return SUCCESS;
}
Example #5
0
vector<NETWORK_ENTRY>
GetSockets(
)
/*++

Routine Description:

    Description.

Arguments:

     - 

Return Value:

    vector<NETWORK_ENTRY>.

--*/
{
    ULONG64 TableAddr;
    ULONG64 TableCountAddr;

    PULONG64 Table = NULL;
    ULONG TableCount;

    vector<NETWORK_ENTRY> NetworkEntries;

    ULONG ProcessorType;
    ULONG PlateformId, Major, Minor, ServicePackNumber;

    if (g_Ext->m_Control->GetActualProcessorType(&ProcessorType) != S_OK) goto CleanUp;
    if (g_Ext->m_Control->GetSystemVersion(&PlateformId, &Major, &Minor, NULL, NULL, NULL, &ServicePackNumber, NULL, NULL, NULL) != S_OK) goto CleanUp;

    // g_Ext->Dml("Major: %d, Minor: %d, ProcessorType = %x\n", Major, Minor, ProcessorType);

    if ((Minor < 6000) && (ProcessorType == IMAGE_FILE_MACHINE_I386))
    {
        if (g_Ext->m_Symbols->GetOffsetByName("tcpip!AddrObjTable", &TableAddr) != S_OK) goto CleanUp;
        if (g_Ext->m_Symbols->GetOffsetByName("tcpip!AddrObjTableSize", &TableCountAddr) != S_OK) goto CleanUp;

        if (ReadPointersVirtual(1, TableAddr, &TableAddr) != S_OK) goto CleanUp;
        if (g_Ext->m_Data->ReadVirtual(TableCountAddr, &TableCount, sizeof(ULONG), NULL) != S_OK) goto CleanUp;

        Table = (PULONG64)malloc(TableCount * sizeof(ULONG64));
        if (ReadPointersVirtual(TableCount, TableAddr, Table) != S_OK) goto CleanUp;

        for (UINT i = 0; i < TableCount; i += 1)
        {
            Network::OBJECT_ENTRY_X86 ObjectEntry = { 0 };

            NETWORK_ENTRY NetworkEntry = { 0 };

            if (Table[i] == 0) continue;

            if (g_Ext->m_Data->ReadVirtual(Table[i], &ObjectEntry, sizeof(Network::OBJECT_ENTRY_X86), NULL) != S_OK) goto CleanUp;

            NetworkEntry.ObjectPtr = Table[i];
            NetworkEntry.CreationTime = ObjectEntry.CreationTime;

            NetworkEntry.ProcessId = ObjectEntry.ProcessId;
            NetworkEntry.Protocol = ObjectEntry.Protocol;

            NetworkEntry.Local.Port = (ObjectEntry.Port[1] << 8) | ObjectEntry.Port[0];
            NetworkEntry.Local.IPv4_Addr[3] = ObjectEntry.LocalAddress[3];
            NetworkEntry.Local.IPv4_Addr[2] = ObjectEntry.LocalAddress[2];
            NetworkEntry.Local.IPv4_Addr[1] = ObjectEntry.LocalAddress[1];
            NetworkEntry.Local.IPv4_Addr[0] = ObjectEntry.LocalAddress[0];

            NetworkEntry.State = TcbListenState;

            NetworkEntries.push_back(NetworkEntry);
        }
    }
    else if (Minor > 6000)
    {
        if (g_Ext->m_Symbols->GetOffsetByName("tcpip!PartitionCount", &TableCountAddr) != S_OK) goto CleanUp;

        ReadPointer(GetExpression("tcpip!PartitionTable"), &TableAddr);
        if (!TableAddr) goto CleanUp;
        if (g_Ext->m_Data->ReadVirtual(TableCountAddr, &TableCount, sizeof(ULONG), NULL) != S_OK) goto CleanUp;

        ULONG ListEntrySize = GetTypeSize("nt!_LIST_ENTRY");
        ULONG PoolHeaderSize = GetTypeSize("nt!_POOL_HEADER");

        ExtRemoteUnTyped PartitionTable(TableAddr, "tcpip!_PARTITION_TABLE");

        for (UINT PartitionIndex = 0; PartitionIndex < TableCount; PartitionIndex += 1)
        {
            NETWORK_ENTRY NetworkEntry = { 0 };

            // g_Ext->Dml("    -> Partition[%d].HashTables = 0x%I64X\n", PartitionIndex, Partition->HashTables);
            ExtRemoteTyped HashTable("(nt!_RTL_DYNAMIC_HASH_TABLE *)@$extin", PartitionTable.ArrayElement(PartitionIndex).Field("HashTables").GetPtr());

            ULONG64 Directory = HashTable.Field("Directory").GetPtr();
            ULONG TableEntries = HashTable.Field("TableSize").GetUlong();

            for (UINT i = 0; i < TableEntries; i += 1)
            {
                ExtRemoteTypedList List(Directory + i * ListEntrySize, "nt!_LIST_ENTRY", "Flink");

                for (List.StartHead(); List.HasNode(); List.Next())
                {
                    ULONG64 Current = List.GetNodeOffset();
                    if (!IsValid(Current)) break;

                    ExtRemoteUnTyped Tcb(Current, "tcpip!_TCB");
                    Tcb.SubtractOffset("HashTableEntry");

                    ExtRemoteTyped PoolHeader("(nt!_POOL_HEADER *)@$extin", Tcb.GetPointerTo() - PoolHeaderSize);
                    if (PoolHeader.Field("PoolTag").GetUlong() != 'EpcT') continue;

                    //# Seen as 0x1f0 on Vista SP0, 0x1f8 on Vista SP2 and 0x210 on 7
                    //# Seen as 0x320 on Win7 SP0 x64
                    ULONG PoolSize;
                    if (PoolHeader.Field("BlockSize").GetTypeSize() == sizeof(USHORT))
                    {
                        PoolSize = PoolHeader.Field("BlockSize").GetUshort() * 0x10;
                    }
                    else
                    {
                        PoolSize = PoolHeader.Field("BlockSize").GetUlong() * 0x10;
                    }

                    if (PoolSize < 0x100) continue;

                    ULONG64 SrcAddress = 0;
                    ULONG64 DstAddress = 0;

                    NetworkEntry.Protocol = PROTOCOL_TCP;
                    NetworkEntry.State = Tcb.Field("State").GetUlong();
                    NetworkEntry.Local.Port = Tcb.Field("LocalPort").GetUshort();
                    NetworkEntry.Remote.Port = Tcb.Field("RemotePort").GetUshort();

                    DstAddress = Tcb.Field("Path", TRUE).Field("DestinationAddress").GetPtr();
                    if (IsValid(Tcb.Field("Path").GetPtr() &&
                        IsValid(Tcb.Field("Path", TRUE).Field("SourceAddress").GetPtr()) &&
                        IsValid(Tcb.Field("Path", TRUE).Field("SourceAddress", TRUE).Field("Identifier").GetPtr()) &&
                        IsValid(Tcb.Field("Path", TRUE).Field("SourceAddress", TRUE).Field("Identifier", TRUE).Field("Address").GetPtr())))
                    {
                        SrcAddress = Tcb.Field("Path", TRUE).Field("SourceAddress", TRUE).Field("Identifier", TRUE).Field("Address").GetPtr();
                    }

                    if (DstAddress && g_Ext->m_Data->ReadVirtual(DstAddress, &NetworkEntry.Remote.IPv4_Addr, sizeof(NetworkEntry.Remote.IPv4_Addr), NULL) != S_OK) goto CleanUp;
                    if (SrcAddress && g_Ext->m_Data->ReadVirtual(SrcAddress, &NetworkEntry.Local.IPv4_Addr, sizeof(NetworkEntry.Local.IPv4_Addr), NULL) != S_OK) goto CleanUp;

                    ExtRemoteTyped OwningProcess("(nt!_EPROCESS *)@$extin", Tcb.Field("OwningProcess").GetPtr());

                    NetworkEntry.ProcessId = OwningProcess.Field("UniqueProcessId").GetPtr();
                    OwningProcess.Field("ImageFileName").GetString((LPSTR)NetworkEntry.ProcessName, sizeof(NetworkEntry.ProcessName));
                    NetworkEntries.push_back(NetworkEntry);
                }
            }
        }
    }

CleanUp:
    if (Table) free(Table);

    return NetworkEntries;
}