Registro del lenguaje C++ e información sobre el tiempo de ejecución
Este ejemplo utiliza el siguiente nombre de archivo: ' runtime.cpp
Código
El código de este ejemplo explora funcionalidades que pueden no ser de uso común. El registro se puede utilizar para ayudar a rastrear la ejecución de un AE. Para que sea útil, el AE debe estar registrado con una máscara de registro y el registro debe estar habilitado a través de nzudxdbg. Para recibir mensajes de registro del sistema en la ventana donde se inició el sistema NPS, es necesario iniciar NPS con la opción '-i' ' (por ejemplo, 'nzstart -i'). Esto hace que los procesos NPS permanezcan unidos al terminal. La información de tiempo de ejecución proporciona estadísticas sobre el sistema NPS, incluyendo el número de dataslices, el número de SPUs y el locus de ejecución.
#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;
}
Una compilación
Utilice la compilación estándar:
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language cpp --template compile \
--exe runtimeae --compargs "-g -Wall" --linkargs "-g" runtime.cpp \
--version 3
Registro
Registra el ejemplo utilizando las opciones --mem y --mask. La opción --mask activa el registro para DEBUG y la opción --mem establece la información de tiempo de ejecución de memoria.
$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
En ejecución
Antes de ejecutarlo, active el registro ejecutando nzudxdbg:
nzudxdbg
Processing 1 spus
.
done
Processing host
done
Ejecute la consulta en nzsql:
SELECT * FROM TABLE WITH FINAL(runtimeae(1,1,1,1,1,1));
OUTPUT
--------
done
(1 row)
El registro del sistema NPS aparece en la ventana donde se inició el sistema NPS:
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
Esta es la salida esperada, ya que los argumentos especificados no coincidían con el tiempo de ejecución en esos casos.