Informazioni di registro e di runtime del linguaggio C++
Questo esempio utilizza il seguente nome di file: 'runtime.cpp
Codice
Il codice di questo esempio esplora funzionalità che potrebbero non essere comunemente utilizzate. La registrazione può essere utilizzata per tracciare l'esecuzione di un AE. Per essere utile, l'AE deve essere registrato con una maschera di log e la registrazione deve essere abilitata tramite nzudxdbg. Per ricevere i messaggi di registrazione del sistema nella finestra in cui è stato avviato il sistema NPS, è necessario avviare NPS con l'opzione '-i' (ad esempio, "nzstart -i'). Questo fa sì che i processi NPS rimangano collegati al terminale. Le informazioni di runtime forniscono statistiche sul sistema NPS, tra cui il numero di dataslices, il numero di SPU e il luogo di esecuzione.
#include <nzaefactory.hpp>
using namespace nz::ae;
static int run(nz::ae::NzaeFunction *aeFunc);
int main(int argc, char * argv[])
{
NzaeApiGenerator helper;
// The following line is only needed if a launcher is not used
helper.setName("testcapi");
for (int i=0; i < 2; i++) {
nz::ae::NzaeApi &api = helper.getApi(nz::ae::NzaeApi::FUNCTION);
run(api.aeFunction);
if (!helper.isRemote())
break;
}
return 0;
}
static void validateRuntime(NzaeFunction *aeFunc, int64_t *args, int type)
{
// argss:
// dataslice, transaction, hardware, numslices, numspus, locus
int64_t dataslice = args[0];
int64_t txid = args[1];
int64_t hwid = args[2];
int64_t nslices = args[3];
int64_t nspus = args[4];
int64_t locus = args[5];
const NzaeRuntime &aeRun = aeFunc->getRuntime();
char buf[1024];
if (locus != (int64_t)aeRun.getLocus())
{
sprintf(buf, "test fails: 1\n");
aeFunc->log(NzaeFunction::LOG_DEBUG, buf);
}
if (aeRun.getSuggestedMemoryLimit() != 123)
{
sprintf(buf, "test fails: 7 \n");
aeFunc->log(NzaeFunction::LOG_DEBUG, buf);
}
if (aeRun.getUserQuery() == false)
{
sprintf(buf, "test fails: 8 \n");
aeFunc->log(NzaeFunction::LOG_DEBUG, buf);
}
if (type != (int64_t)aeRun.getAdapterType())
{
sprintf(buf, "test fails: 9 \n");
aeFunc->log(NzaeFunction::LOG_DEBUG, buf);
}
if (locus == (int64_t)NzaeRuntime::NZAE_LOCUS_POSTGRES) {
return;
}
if (dataslice != (int64_t)aeRun.getDataSliceId())
{
sprintf(buf, "test fails: 2 \n");
aeFunc->log(NzaeFunction::LOG_DEBUG, buf);
}
if (txid != 0 && txid != (int64_t)aeRun.getTransactionId())
{
sprintf(buf, "test fails: 3 \n");
aeFunc->log(NzaeFunction::LOG_DEBUG, buf);
}
if (hwid != (int64_t)aeRun.getHardwareId())
{
sprintf(buf, "test fails: 4 \n");
aeFunc->log(NzaeFunction::LOG_DEBUG, buf);
}
if (nslices != (int64_t)aeRun.getNumberDataSlices())
{
sprintf(buf, "test fails: 5 \n");
aeFunc->log(NzaeFunction::LOG_DEBUG, buf);
}
if (nspus != (int64_t)aeRun.getNumberSpus())
{
sprintf(buf, "test fails: 6 \n");
aeFunc->log(NzaeFunction::LOG_DEBUG, buf);
}
}
class MyHandler : public NzaeFunctionMessageHandler
{
public:
void evaluate(NzaeFunction& api, NzaeRecord &input, NzaeRecord &result) {
int64_t ar[6];
for (int i=0; i < 6; i++)
{
nz::ae::NzaeField &fi = input.get(i);
if (fi.type() == NzaeDataTypes::NZUDSUDX_INT64){
ar[i] = (int64_t)(NzaeInt64Field&)fi;
}
else if (fi.type() == NzaeDataTypes::NZUDSUDX_INT32){
ar[i] = (int32_t)(NzaeInt32Field&)fi;
}
}
validateRuntime(&api, ar, NzaeRuntime::NZAE_ADAPTER_UDTF);
std::string str = "done";
NzaeStringField &f = (NzaeStringField&)result.get(0);
f = str;
}
};
static int run(NzaeFunction *aeFunc)
{
aeFunc->run(new MyHandler());
return 0;
}
Compilazione
Utilizzare la compilazione standard:
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language cpp --template compile \
--exe runtimeae --compargs "-g -Wall" --linkargs "-g" runtime.cpp \
--version 3
Registrazione
Registrate l'esempio usando le opzioni --mem e --mask. L'opzione --mask abilita la registrazione per DEBUG e l'opzione --mem imposta le informazioni sulla memoria di runtime.
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae \
--sig "runtimeae(int8, int8, int8,int8,int8,int8)" \
--return "table(output varchar(100))" \
--language cpp --template udtf --exe runtimeae --version 3 \
--mem 123 --mask debug
In esecuzione
Prima dell'esecuzione, abilitare la registrazione eseguendo nzudxdbg:
nzudxdbg
Processing 1 spus
.
done
Processing host
done
Eseguire la query in nzsql:
SELECT * FROM TABLE WITH FINAL(runtimeae(1,1,1,1,1,1));
OUTPUT
--------
done
(1 row)
La registrazione del sistema NPS appare nella finestra in cui il sistema NPS è stato avviato:
03-19-10 09:43:59 (dbos.3020) [d,udx ] test fails: 2
03-19-10 09:43:59 (dbos.3020) [d,udx ] test fails: 3
03-19-10 09:43:59 (dbos.3020) [d,udx ] test fails: 4
Questo è l'output previsto, poiché in questi casi gli argomenti specificati non corrispondono al runtime.