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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include "streamlet-common.h"
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <unistd.h>
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
/**
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
 * This streamlet is useful only for debugging or study purposes.
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
 * 
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
 * It provides two attributes:
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
 *  - pid_streamlet: getpid()
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
 *  - pid_worker: getppid()
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
 *	
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
 * Can be used for studying how the parallelism works:
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
 *  - Each instance of streamlet (each --streamlet pid) will have different pid_streamlet.
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
 *  - Each worker (their number = X in --parallelism X) will have different pid_worker.
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
 * 
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
 * So we can check how evenly the work has been distributed across processes (e.g. do some GROUP BY in SQL).
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
 */
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
class PidStreamlet : public Streamlet {
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	std::vector<AttributeMetadata> getOutputAttributesMetadata() override {
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
		std::vector<AttributeMetadata> oam;
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
		int i = 0;
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
		oam.push_back({getAlias(i++, L"pid_streamlet"), INTEGER});
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
		oam.push_back({getAlias(i++, L"pid_worker"), INTEGER});
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		return oam;
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	}
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	std::vector<OutputAttribute> getOutputAttributes() override {
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		std::vector<OutputAttribute> oa;
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		oa.push_back({std::to_wstring(getpid()), false});
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		oa.push_back({std::to_wstring(getppid()), false});
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		return oa;
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	}
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
};
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
7ba9d703fadb streamlet examples: pid
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
STREAMLET_RUN(PidStreamlet)