Enregistrement du langage C++ et informations sur le temps d'exécution
Cet exemple utilise le nom de fichier suivant : " runtime.cpp
Coder
Le code de cet exemple explore des fonctionnalités qui ne sont pas forcément utilisées couramment. La journalisation peut être utilisée pour retracer l'exécution d'un AE. Pour être utile, l'AE doit être enregistré avec un masque de journalisation et la journalisation doit être activée via nzudxdbg. Pour recevoir les messages de journalisation du système dans la fenêtre où le système NPS a été démarré, vous devez démarrer NPS avec l'option '-i' (par exemple, "nzstart -i'). Les processus NPS restent ainsi attachés au terminal. Les informations sur l'exécution fournissent des statistiques sur le système NPS, notamment le nombre de tranches de données, le nombre de SPU et le lieu d'exécution.
#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;
}
Compilation
Utiliser la compilation standard :
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language cpp --template compile \
--exe runtimeae --compargs "-g -Wall" --linkargs "-g" runtime.cpp \
--version 3
Enregistrement
Enregistrez l'exemple en utilisant les options --mem et --mask. L'option --mask active la journalisation pour DEBUG et l'option --mem définit les informations sur la durée d'exécution de la mémoire.
$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 cours d'exécution
Avant de commencer, activez la journalisation en exécutant nzudxdbg:
nzudxdbg
Processing 1 spus
.
done
Processing host
done
Exécutez la requête dans nzsql :
SELECT * FROM TABLE WITH FINAL(runtimeae(1,1,1,1,1,1));
OUTPUT
--------
done
(1 row)
L'enregistrement du système NPS apparaît dans la fenêtre où le système NPS a été démarré :
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
C'est la sortie attendue puisque les arguments spécifiés ne correspondaient pas à la durée d'exécution dans ces cas.