streamlet-examples/pid.cpp
author František Kučera <franta-hg@frantovo.cz>
Fri, 13 May 2022 21:35:30 +0200
branchv_0
changeset 96 c34106244a54
parent 64 7ba9d703fadb
permissions -rw-r--r--
portable order of (i++) parameters

/**
 * Relational pipes
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "streamlet-common.h"

#include <unistd.h>

/**
 * This streamlet is useful only for debugging or study purposes.
 * 
 * It provides two attributes:
 *  - pid_streamlet: getpid()
 *  - pid_worker: getppid()
 *	
 * Can be used for studying how the parallelism works:
 *  - Each instance of streamlet (each --streamlet pid) will have different pid_streamlet.
 *  - Each worker (their number = X in --parallelism X) will have different pid_worker.
 * 
 * So we can check how evenly the work has been distributed across processes (e.g. do some GROUP BY in SQL).
 */
class PidStreamlet : public Streamlet {

	std::vector<AttributeMetadata> getOutputAttributesMetadata() override {
		std::vector<AttributeMetadata> oam;
		int i = 0;
		oam.push_back({getAlias(i++, L"pid_streamlet"), INTEGER});
		oam.push_back({getAlias(i++, L"pid_worker"), INTEGER});
		return oam;
	}

	std::vector<OutputAttribute> getOutputAttributes() override {
		std::vector<OutputAttribute> oa;
		oa.push_back({std::to_wstring(getpid()), false});
		oa.push_back({std::to_wstring(getppid()), false});
		return oa;
	}
};

STREAMLET_RUN(PidStreamlet)