streamlet-examples/pid.cpp
branchv_0
changeset 64 7ba9d703fadb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/streamlet-examples/pid.cpp	Mon Jan 27 00:03:27 2020 +0100
@@ -0,0 +1,53 @@
+/**
+ * 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)